Presentation is loading. Please wait.

Presentation is loading. Please wait.

PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.

Similar presentations


Presentation on theme: "PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU."— Presentation transcript:

1 PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh kyshieh@pllab.cs.nthu.edu.tw Programming Language Lab., NTHU

2 PLLab, NTHU,Cs2403 Programming Languages Expressions Expressions are the fundamental means of specifying computations in a programming language Expression evaluation depends on the orders of operator and operand evaluation Issues: –Precedence –Associativity –Order of operand evaluation –Side effects –…

3 PLLab, NTHU,Cs2403 Programming Languages Operators A unary operator has one operand e.g. ++ A binary operator has two operands e.g. +, -, *, / A ternary operator has three operands e.g. ?:

4 PLLab, NTHU,Cs2403 Programming Languages Operator precedence Precedence rules define the order in which “ adjacent ” operators of different precedence levels are evaluated. Ex: a = 10; b = 3; c = 4; d = a + b * c; d (in C) =22

5 PLLab, NTHU,Cs2403 Programming Languages Operator associativity Associativity rules define the order in which adjacent operators with the same precedence level are evaluated Associativity in most PL is left-to-right, except for exponentiation op (right-to-left) Ex: a = 10; b = 3; c = 4; d = a – b + c; d (in C) = 7

6 PLLab, NTHU,Cs2403 Programming Languages Parentheses Precedence and associativity rules can be overriden with parentheses a = 10; b = 3; c = 4; d = (a + b) * c; d (in C) = 52 Use parentheses when not clear in your code to increase readability Ex: a = 10; b = 3; c = 4; d (in C) = a**b**c = a**(b**c) = 10**81

7 PLLab, NTHU,Cs2403 Programming Languages Functional side effects Side effect of a function occurs when a function changes one of its parameter or global variable Ex: int a = 5; int fun1() { a = 17; return 3; } int fun2(){ a = a + fun1(); } void main() { fun2(); } a (in C) = 8

8 PLLab, NTHU,Cs2403 Programming Languages Operator overloading Use of an operator for more than one purpose Increase readability but also decrease readability EX: –Matrix operation: A * B + C, instead of MatrixAdd( MatrixMult( A, B ), C ) –A + B is actually implemented as (A AND B)

9 PLLab, NTHU,Cs2403 Programming Languages Type Conversions Narrowing conversion converts an object to a type that cannot include all of the values of the original type e.g., float to int Widening conversion convets an object to a type that can include at least approximations to all of the values of the original type e.g., int to float Coercion is an implicit type conversion

10 PLLab, NTHU,Cs2403 Programming Languages Relational and Boolean Expressions Compares the values of its operands Evaluate to some Boolean representation C has no Boolean type--it uses int type with 0 for false and nonzero for true Ex: a = 3, b = 4, c = a + 1 > 2 * b c = 0;

11 PLLab, NTHU,Cs2403 Programming Languages Short Circuit Evaluation The result of an expression is determined without evaluating all the operands and/or operators EX: while ((index < length) && (list[index] != key)) { Index = index + 1; } Table lookup

12 PLLab, NTHU,Cs2403 Programming Languages Assignment Statements The operator symbol: –= FORTRAN, BASIC, PL/I, C, C++, Java –:= ALGOLs, Pascal, Ada Unary assignment ops –++count; (count = count + 1;) Undetected error in C/C++ –if (x == y) is different from if (x = y) … –Java only allows Boolean expressions in if statements

13 PLLab, NTHU,Cs2403 Programming Languages Control structure Control structure is a control statement and the statements whose execution it controls Selection statement provides the means of choosing between two or more paths of execution –Two-Way Selection Statements –Multi-Way Selection Statements

14 PLLab, NTHU,Cs2403 Programming Languages Two-Way Selection Statements if-then-else –matching else problem rules: else matches the most recent then –used in Java and most other imperative PL (C, Pascal) ALGOL 60: use compound statement for nesting if Ada: use “end if” reserve word

15 PLLab, NTHU,Cs2403 Programming Languages Multiple selection constructs FORTRAN: IF (exp) N1, N2, N3 FORTRAN: GO TO (lb1, lb2, …, lbN), exp Pascal: case (exp) of … end C, C++: switch (index) { case … break… default …}

16 PLLab, NTHU,Cs2403 Programming Languages Iterative Statements repeated execution of a statement or compound statement is accomplished either by iteration or recursion Counter-Controlled Loops Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 4. Should the loop parameters be evaluated only once, or once for every iteration?

17 PLLab, NTHU,Cs2403 Programming Languages examples Iterative statements –FORTRAN 90: DO lavel var = init, final [,step] –ALGOL 60: for count := 1 step 1 until 10 do for index := 1, 4, 13, 41 step 2 until 47, 3 * index while index < 1000, 34, 2, -24, do  1, 4,13, 41,43, 45, 47, 147, 441, 34, 2, -24 –Pascal: for var := init (to | downto) final do –Ada: for k in 1..10 loop –C, C++, Java: for (k = 0; k <= 10; k++) {…} Iteration on data list –Perl: @names = {“Bob”, “John”, “Tom”}; foreach $name (@names) { print $name; }

18 PLLab, NTHU,Cs2403 Programming Languages Unconditional Branching Problem: readability Some languages do not have them: e.g., Java Loop exit statements are restricted and somewhat camouflaged goto ’ s

19 PLLab, NTHU,Cs2403 Programming Languages Guarded statements Guarded commands –suggested by Dijkstra (1975) –all boolean exps are evaluated –one of the true-stmts is nondeterministically chose for execution –if all exps are false, run-time error occurs if i = 0 -> sum := sum + i; [ ] i > j -> sum := sum + j; [ ] i sum := sum + i; fi

20 PLLab, NTHU,Cs2403 Programming Languages Guarded statements do -> [ ] -> [ ] … [ ] -> od –all boolean exps are evaluated on each iteration –one of the true stmts are nondeterministically executed, and then the loop is evaluated again –when all exps are false, the loop terminates sort q1 to q4 such that q1 <= q2 <= q3 <= q4 do q1 > q2 -> temp := q1; q1 := q2; q2 := temp; [ ] q2 > q3 -> temp := q2; q2 := q3; q3 := temp; [ ] q3 > q4 -> temp := q3; q3 := q4; q4 := temp; od


Download ppt "PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU."

Similar presentations


Ads by Google