A3 Control Flow
A3 Control Flow
Most imperative languages use infix notation for binary operators, and prefix notation
for many operators and other functions.
Precedence says that certain operators, in the absence of parentheses, group more tightly than other
operators, e.g.
a-b*c is a-(b*c)
Associativity says sequences of operators of equal precedence, in the absence of parentheses, group
to the left or to the right, e.g.
a-b+c is (a-b)+c
2110316 PROG LANG PRIN: CONTROL FLOW 3
Precedence
In most languages ,
multiplication and division
group more tightly than
addition and subtraction.
Fortrana + b * c ** d ** e / f result is
Similarly, in f(a, g(b), h(c)) , what is the order in which the arguments will be evaluated?
Be careful when writing expression in which side effect of evaluating one operand or
argument can affect the value of another, e.g. use parentheses to impose ordering.
But Java and C# require left-to-right evaluation (i.e. cleaner semantics over run time
cost).
int main() {
printf("%d\n", give4() + give2() * give3() - give4() / give2());
return 0;
}
2110316 PROG LANG PRIN: CONTROL FLOW 9
Assignment
In imperative language, assignment provides the means to make the changes to the
values of variable in memory.
Assignment takes two arguments.
• A value
• A reference to a variable into which the value should be placed.
Assignment has a side effect, i.e. it changes the value of a variable, thereby affecting
the result of any later computation in which the variable appears.
//C --Haskell has no assignment and no side effect
int max(int x, int y) { a, b :: Int
if (x > y) {return x;} else {return y;} a = 1
} b = 2
int main() max a b
{ int a, b; --2
a = 1; b = 2; max a b
printf("max is %d\n", max(a, b)); //max is 2 --2
a = 3;
printf("max is %d\n", max(a, b)); //max is 3
return 0;
}
10
Semantics of Assignment (1)
In value model of variables, a variable is a named container for a value (e.g. Pascal,
C, Java’s built-in type, PHP).
A variable has two interpretations when used with assignment.
• l-value refers to expression that denotes location.
• r-value refers to expression that denotes value.
//C
b = 2; // l-value of b is used
c = b; // r-value of b is used, l-value of c is used
a = b+c; // r-values of b and c are used, l-value of a is used
13
https://www.quora.com/How-can-I-implement-short-circuit-evaluation-and-operators-for-a-JVM-based-language-compiler-
2110316 PROG LANG PRIN: CONTROL FLOW
Short-Circuit Changes Semantics of Boolean Expressions
An example of a search for an element in a list.
my_list
key next key next
p
C short-circuits its logical operators.
//C
p = my_list;
while (p && p->key != val)
p = p->next;
Pascal does not short-circuit. Both <> will be evaluated before and, so run-time
semantic error if p is nil (unsuccessful search).
(* Pascal *)
p := my_list;
while (p <> nil) and (p^.key <> val) do (*ouch!*)
p := p^.next;
< min
> max
r1 := T[r1]
goto *r1
content of r1 21
Exercise: Case/Switch Implementation
What is the problem with jump table implementation in the previous slide?
Mid-test loop: A special statement is nested inside a test for terminating condition.
//C
for (;;) {
line = read_line(stdin);
if (all_blanks(line)) break;
consume_line(line);
}
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + “ ”);
}
…
Contents of al: C A E
2110316 PROG LANG PRIN: CONTROL FLOW 28
Recursion
Functions calling themselves.
Functions calling other functions that call them back in turn.
Any iterative algorithm can be rewritten as a recursive algorithm and vice versa.
Which to use in which circumstance is mainly a matter of taste.
//C //C
//Iteration, assume a, b > 0 //Recursion, assume a, b > 0
int gcd(int a, int b) { int gcd(int a, int b) {
while (a != b) { if (a == b) return a; //base case
if (a > b) a = a-b; else if (a > b) return gcd (a-b, b);
else b = b-a; else return gcd(a, b-a);
} }
return a;
}