Question

In: Computer Science

>. Briefly explain how operator precedence works in APL. > What does times do in Ruby?...

>. Briefly explain how operator precedence works in APL.

> What does times do in Ruby? What about each? What about upto? Use code snippets to explain your answers.

> What is a functional side effect? Give an example in code.  

> What is an unconditional branch statement? If you were to define a new language, would you include this construct? Why or why not?

> Read about how flexible for-loops are in C. Write two examples of "non-traditional" for-loops (ie, for-loops that don't look like for-loops you learned in introductory courses) and explain how they work.

Solutions

Expert Solution

Answer:

1. APL is a programming language where operator precedence describes the order in which operations are performed when an expression is evaluated. Operations with the higher precedence are performed before those having lower precedence.

Following table shows operator precedence:

Description Operator Associativity
Function expression () Left to right
Array Expression [] Left to right
Structure operators -> Left to right
Unary minus - Right to left
Increment & decrement --,++ Right to left
One's complement ~ Right to left
Pointer Operators &* Right to left
Type cast (data type) Right to left

Arithmetic Operators:

Multiplication,Divide,Modulus *,/,% Left to right
Add,Subtract +,- Left to right

Relational Operators:

Less than < Left to right
Greater than > Left to right
Less than equal to <= Left to right
Greater than equal to >= Left to right
Equal to == Left to right
Not equal != Left to right

Logical operators:

AND && Left to right
OR || Left to right
NOT ! Right to left

Bitwise operators:

AND & Left to right
Exclusive OR ^ Left to right
Inclusive OR | Left to right

Other operators:

Comma , Right to left
Conditional operator ?: Right to left

2.Functions of a language RUBY:

times(): The 'times function' in ruby returns all the numbers from 0 to one less than the number itself.It iterates the given block, passing in increasing values from 0 up to the limit.

Syntax:

(number).times

each():The each() is an inbuilt method in Ruby iterates over every element in the range.

Syntax:range.each(|variable name| block)

Example:

[1, 2, 3].each { |n| puts "Current number is: #{n}" }
Output
Current number is: 1
Current number is: 2
Current number is: 3

upto():Upto executes the block given once for each number from the original number "upto" the argument passed.

Example:

1.upto(5) {|n| puts n}

This prints:

1 2 3 4 5

3.Functional Side Effect:

A functional side effect is when a function changes a two-way parameter or a nonlocal variable. It occurs when the function changes either one of its parameters or a global variable. Consider this example, n + fun(n). If 'fun' changes n, there is a side effect. This can cause many problems when a program is trying to do calculations. Many times there is a possibility of two or more outputs.

Solution to functional side effect:

The first solution is that the designer could disallow function evaluation from affecting the value of expressions by disallowing functional side effects. They can state that there are no two-way parameters in functions and no nonlocal references in functions. This solution is good because it will always work. On the other side, disallowing functional side effects can sometimes be difficult. As a result, it eliminates some of the programmer's flexibility.

The second solution to side effect problems is to state in the language definition that operands in expressions are to be written and evaluated in a specific order and demand that implementers guarantee that order. The problem that arises from this is that some coding techniques used by compilers involve recording operand evaluations. A specified order disallows those optimization methods when function calls are involved. This solution limits some compiler optimizations.

4.Unconditional Branch Statements are the statements , when the programmer forces the execution of a program to jump to another part of the program.

We can do this by using:

  • The goto statement is used for unconditional branching or transfer of the program execution to the labeled statement.

    Syntax

    The syntax for a goto statement in C is as follows −

    goto label;
    ..
    .
    label: statement;
Example:
#include <stdio.h>
 
int main () {

   /* local variable definition */
   int a = 10;

   /* do loop execution */
   LOOP:do {
   
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         goto LOOP;
      }
  
      printf("value of a: %d\n", a);
      a++;
     
   }while( a < 20 );
 
   return 0;
}

If i will be going to create a new language than i will include this construct beause this statements are helpful in many aspects while coding.

5.

Flexibility of for loop:

As mentioned above, C's for construct is quite versatile. You can use almost any statement you like for its initialization, condition, and increment parts, including an empty statement. For example, omitting the initialization and increment parts creates what is essentially a while loop:

int my_int = 1;

for ( ; my_int <= 20; )
{
  printf ("%d ", my_int);
  my_int++;
}

Omitting the condition part as well produces an infinite loop, or loop that never ends:

for ( ; ; )
{
  printf("Aleph Null bottles of beer on the wall...\n");
}

You can break out of an "infinite loop" with the break or return commands. (See Terminating and speeding loops.)

Consider the following loop:

for (my_int = 2; my_int <= 1000; my_int = my_int * my_int)
{
  printf ("%d ", my_int);
}

This loop begins with 2, and each time through the loop, my_int is squared.

Here's another odd for loop:

char ch;

for (ch = '*'; ch != '\n'; ch = getchar())
{
  /* do something */
}

This loop starts off by initializing ch with an asterisk. It checks that ch is not a linefeed character (which it isn't, the first time through), then reads a new value of ch with the library function getchar and executes the code inside the curly brackets. When it detects a line feed, the loop ends.

It is also possible to combine several increment parts in a for loop using the comma operator ,. (See The comma operator, for more information.)

#include <stdio.h>

int main()
{
  int up, down;

  for (up = 0, down=10; up < down; up++, down--)
  {
    printf("up = %d, down= %d\n",up,down);
  }

  return 0;
}

The example above will produce the following output:

up = 0, down= 10
up = 1, down= 9
up = 2, down= 8
up = 3, down= 7
up = 4, down= 6

One feature of the for loop that unnerves some programmers is that even the value of the loop's conditional expression can be altered from within the loop itself:

int index, number = 20;

for (index = 0; index <= number; index++)
{
  if (index == 9)
  {
    number = 30;
  }
}

In many languages, this technique is syntactically forbidden. Not so in the flexible language C. It is rarely a good idea, however, because it can make your code confusing and hard to maintain.

Two more looping statements that works same as for loop:

  • While: A loop is used for executing a block of statements repeatedly until a given condition returns false.

Example of while loop

#include <stdio.h>
int main()
{
   int count=1;
   while (count <= 4)
   {
        printf("%d ", count);
        count++;
   }
   return 0;
}
  • 'do-while' loop:A do while loop is similar to while loop with one exception that it executes the statements inside the body of do-while before checking the condition.
  • Example of do while loop
  • #include <stdio.h>
    int main()
    {
            int j=0;
            do
            {
                    printf("Value of variable j is: %d\n", j);
                    j++;
            }while (j<=3);
            return 0;
    }

Hope this will be helpful for you................Thanks.


Related Solutions

briefly explain how heat engine works
briefly explain how heat engine works
Briefly explain how the hype cycle works.
Briefly explain how the hype cycle works.
WHAT IS Coordination services: AND HOW DOES IT WORKS
WHAT IS Coordination services: AND HOW DOES IT WORKS
Explain how an automatic pipette (like the one pictured) below works. Briefly explain how to use...
Explain how an automatic pipette (like the one pictured) below works. Briefly explain how to use it How can you accurately aliquot a highly viscous solution using an automatic pipette? What does the term “specific gravity” mean? What is the specific gravity of a 50% (v/v) ethanol (in water) solution?
Briefly explain how a compression molding press works and name the major components.
Briefly explain how a compression molding press works and name the major components.
1. Explain how the magnetosphere of Saturn works. How does it compare with Earth's? How is...
1. Explain how the magnetosphere of Saturn works. How does it compare with Earth's? How is it similar and different? Is it smaller or bigger and by how much? Use diagrams to help illustrate your explanations.
Briefly explain what changing the gravitational force will do to friction on level ground.  Why does this...
Briefly explain what changing the gravitational force will do to friction on level ground.  Why does this happen?  How will this change on a hill?
What is the function (what is it, what does it do, how does it do it)...
What is the function (what is it, what does it do, how does it do it) of a defrost high limit thermostat and a defrost termination thermostat?
How does the diuretic works?
How does the diuretic works?
how does a bond works?
how does a bond works?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT