Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.

Similar presentations


Presentation on theme: "Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application."— Presentation transcript:

1 Expressions and Statements

2 Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application of operators and functions An operator that takes one operand is an unary operator An operator that takes two operands is a binary operator

3 Operator Notation Prefix notation – the operator precedes its operands + 3 * 4 5 Infix notation – the operator is between its operands 3 + 4 * 5 Postfix notation – the operator follows its operands 3 4 5 * +

4 Expression Tree + 3* 4 5

5 Associativity and Precedence Infix notation requires rules for associativity and precedence. Parentheses are used to alter these rules In Prefix and Postfix notation, there is no need for such rules. The notation itself shows explicitly the associativity nd precedence of the operators in the expression

6 Operators and Function Many languages make a distinction between operators and functions, while others do not. We could write the expression 3+4*5 with user-defined functions mul and add add(3, mul(4, 5)) This is very much like prefix notation

7 Other Languages Stack based languages, like FORTH and PostScript use postfix notation exclusively Some functional languages use prefix notation –Lisp (and Scheme) use prefix notation, but the expressions must be fully parenthesized since Lisp operators can take a variable number of operands

8 Applicative Order Evaluation All operands are evaluated first, and then the operator is applied to the results This corresponds to the “bottom-up” evaluation of the values in an expressions syntax tree What about the order of evaluation of sub- expression? –Many languages state that there is no specified order of evaluation of arguments to user-defined functions

9 #include int x = 1; int f(void){ x += 1; return x; } int p( int a, int b){ return a + b; } main(){ printf("%d\n",p(x,f())); return 0; }

10 Side effects A side effect is a change to the environment caused by a function call If there are no side effects, the order of evaluation of parameters does not matter Assignment is the quintessential side-effect. In C, assignment is an expression x = y = z + w; –Assignment has low precedence and right associativity

11 Short-circuit Evaluation of Boolean Expressions Boolean expressions are evaluated left to right until their truth value becomes known (then evaluation stops) (x != 0) && (y/x > 5) is ok, while (y/x > 5) && (x != 0) is not Many languages require short-circuiting, or at least provide for it (Pascal does not!) –ML uses keywords andalso and orelse –In Ada, and and or are not short-circuited, but “ and then ” and “ or else ” are.

12 Other short-circuitry The if operator is a ternary operator (three operands), and only two of these are evaluated ML: if e 1 then e 2 else e 3 C: e 1 ? e 2 : e 3 –If e 1 evaluates to true then the result is e 2, and e 3 is not evaluated –If e 1 evaluates to false then the result is e 3, and e 2 is not evaluated Case expressions are similar

13 Normal Order Evaluation Each operation or function begins its operation before its operands are evaluated, Each operand is evaluated only as it is needed for the calculation of the result Normal order evaluation may give different results than applicative order evaluation, especially if there are side effects

14 Guarded if Statements if B1 -> S1 | B2 -> S2 | B3 -> S3 … | Bn -> Sn fi Bi ’s are boolean expressions, called guards Si ’s are statement sequences Choose one of the Bi ’s that is true and execute its statement sequence It is an error for all Bi ’s to evaluate to false

15 If statements EBNF for an if statement (in C) if-stmt  if (expr) stmt [else stmt] where stmt can be a single statement or a sequence of statements in braces

16 Syntactical Ambiguity The statement if (e1) if (e2) S1 else S2 is ambiguous This ambiguity is known as the dangling else problem Many languages (such as C and Pascal) handle the problem with a disambiguating rule: –The else part is associated with the closest prior if that does not already have an else part

17 Other Approaches Ada uses a bracketing keyword: if-stmt  if (expr) stmt-sequence [else stmt-sequence] end if; Algol68 used fi as the bracketing keyword Both of these “pile up” the ending keyword(s) if there are multiple nested ifs –One improved approach is to allow a keyword elsif instead of else if to avoid this problem

18 Case and Switch Statements C. A. R. Hoare invented the case statement as a special kind of guarded if statement, where the guards are ordinal values that are selected by an ordinal expression This case statement is the basis for the switch statement of C and Java

19 Switch in C Each case label is treated syntactically as a statement label Without a break, execution falls through to the next case There is an optional default case, if the value of the controlling expression is not found in one of the cases

20 case statement in Ada Case values may be grouped together –Listing, separated by |’s –Listing as a range ( 2.. 5 ) Case values must be distinct Case values must be exhaustive –If there is no default case (when others), and if a legal value is not listed, there is a compile time error

21 Ada case Example case x - 1 is when 0 => y := 0; z := 2; when 2.. 5 => y := 3; z := 1; when 7 | 9 => z := 10; when others => null; end case;

22 ML case expression Case is an expression, returning a value The _ pattern is a wildcard pattern, serving as a default case fun casedemo x = case x - 1 of 0 => 2 | 2 => 1 | _ => 10 ;

23 Guarded do Statements do B1 -> S1 | B2 -> S2 | B3 -> S3 … | Bn -> Sn Od Bi ’s are boolean expressions, called guards Si ’s are statement sequences Select any Bi which is true and execute its statement Repeat until all the Bi’s are false

24 While Loops Java, C, C++ while (e) S Ada while e loop S1 … Sn end loop

25 do while A do while loop is a bottom tested loop do S while (e) This is an example of “syntactic sugar”

26 Multiple Exit Points C and Java allow a break statement in a loop as an alternate way to exit a loop Ada has a similar statement, exit when e;

27 for loops C, C++, Java for ( int i = 0; i < size; i++) sum += a[i]; Ada for i in 0.. size-1 loop sum := sum + a(i); end loop;

28 Typical Restrictions The value of the loop counter cannot be altered in the body of the loop The loop counter is undefined after loop termination The loop counter must be of restricted types There are restrictions on how the loop counter may be declared –Not a paremeter to a procedure –Not a member of a record or structure –Perhaps it must be locally declared

29 Other questions regarding for loops May the bounds be changed inside the body of the loop? If the upper bound is less than the lower bound, does the loop execute at all? Is the loop variable undefined even if the loop is terminated by break or exit ? What checks does the translator perform on the loop structure?

30 The GOTO controversy Bohm and Jacopini (1966) shoed theroetically that GOTO’s were unnecessary. Edsgar Dijkstra (1968) published a letter to the editor in the Communications of the ACM arguing that the use of GOT is harmful.

31 Controversy continued Rudin (1987) published a letter to the editor of the Communications of the ACM “ ‘Gotos considered harmful’ considered harmful” –He argued that academicians were doing a disservice by not teaching the proper use of GOTOs

32 Alternatives to GOTO Many modern languages provide alternative constructs which perform like “disciplined” GOTOs –Labeled break statements –Labeled continue statements


Download ppt "Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application."

Similar presentations


Ads by Google