Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 452: Programming Languages Expressions and Control Flow.

Similar presentations


Presentation on theme: "CSE 452: Programming Languages Expressions and Control Flow."— Presentation transcript:

1 CSE 452: Programming Languages Expressions and Control Flow

2 2 Organization of Programming Languages-Cheng (Fall 2004) Outline of Today’s Lecture u Expressions and Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions  Short-circuit evaluation Assignment Statements  Mixed mode assignment

3 3 Organization of Programming Languages-Cheng (Fall 2004) Expressions u Expressions are the fundamental means of specifying computations in a programming language u Types: Arithmetic Relational/Boolean

4 4 Organization of Programming Languages-Cheng (Fall 2004) Arithmetic Expressions u Consist of operators, operands, parentheses, and function calls u Design issues for arithmetic expressions: What are the operator precedence rules? What are the operator associativity rules? What is the order of operand evaluation? Are there restrictions on operand evaluation side effects? Does the language allow user-defined operator overloading? What mode mixing is allowed in expressions?

5 5 Organization of Programming Languages-Cheng (Fall 2004) Arithmetic Expressions u Types of operators A unary operator has one operand: - x A binary operator has two operands: x + y  Infix: operator appears between two operands  Prefix: operator precede their operands A ternary operator has three operands: (x > 10)? 0 : 1 u Evaluation Order Operator evaluation order Operand evaluation order

6 6 Organization of Programming Languages-Cheng (Fall 2004) Operator Evaluation Order u Four rules to specify order of evaluation for operators 1. Operator precedence rules  Define the order in which the operators of different precedence levels are evaluated (e.g., + vs * ) 2. Operator associativity rules  Define the order in which adjacent operators with the same precedence level are evaluated (e.g., left/right associative) 3. Parentheses  Precedence and associativity rules can be overriden with parentheses 4. Conditional Expressions ( ?: operator in C/C++/Perl)  Equivalent to if-then-else statement

7 7 Organization of Programming Languages-Cheng (Fall 2004) Operand Evaluation Order u When do we evaluate operand? Variables are evaluated by fetching their values from memory Constants  Sometimes, constants are evaluated by fetching its value from memory;  At other times, it is part of the machine language instruction Parenthesized expressions  If operand is a parenthesized expression, all operators it contains must be evaluated before its value can be used as an operand Function calls  Must be evaluated before its value can be used as an operand

8 8 Organization of Programming Languages-Cheng (Fall 2004) Operand Evaluation Order u Functional Side Effects Occurs when the function changes one of its parameters or global variable a + fun(a)  If fun does not have the side effect of changing a, then the order evaluation of the two operands, a and fun(a), does not matter  If fun does have the side effect of changing a, order of evaluation matters Two Possible Solutions :  Disallow functional side effects in the language definition No two-way parameters in functions No non-local references in functions Advantage: it works! Disadvantage: No more flexibility  Write the language definition to demand that operand evaluation order be fixed Disadvantage: limits some compiler optimizations

9 9 Organization of Programming Languages-Cheng (Fall 2004) Overloaded Operators u Multiple use of an operator E.g., use + for integer addition and floating-point addition u Some drawbacks of operator overloading May affect readability  E.g., the ampersand (&) operator in C is used to specify bitwise logical AND operation Address of a variable May affect reliability  Program does not behave the way we want  int x, y; float z; z = x / y  Problem can be avoided by introducing new symbols (e.g., Pascal’s div for integer division and / for floating point division) u C++ and Ada allow user-defined overloaded operators Potential problems:  Users can define nonsense operations  Readability may suffer, even when the operators make sense E.g., use + to mean multiplication

10 10 Organization of Programming Languages-Cheng (Fall 2004) Type Conversions u Narrowing conversion converts the value of a type to another type that cannot store all the values of the original type e.g., convert double to float u Widening conversion converts the value to a type that include at least approximations to all of the values of the original type e.g., convert integers to float

11 11 Organization of Programming Languages-Cheng (Fall 2004) Type Conversions u Implict/Explicit type conversion Coercion is an implicit type conversion  Useful for mixed-mode expression, which contains operands of different types  Disadvantage: decreases type error detection ability of compilers  In most languages, all numeric types are coerced in expressions, using widening conversions  In Ada, there are virtually no coercions in expressions Explicit Type Conversions  Often called type casts  Ada: Float(Index) -- Index is originally an integer type  Java: (int) speed /* speed is float type */

12 12 Organization of Programming Languages-Cheng (Fall 2004) Relational Expressions u Relational operator is an operator that compares the values of its two operands u Relational expression has two operands and one relational operator u Operator symbols used vary somewhat among languages  Ada: /= (not equal operator)  C-based language: !=  Fortran.NE. or <> u Javascript and PHP has two additional relational operators: === and !== similar to == and !=, except it is used to prevent coercion E.g., “7” == 7 is true in Javascript but “7”===7 is false

13 13 Organization of Programming Languages-Cheng (Fall 2004) Boolean Expressions u Consist of Boolean variables, Boolean constants, relational expressions, and Boolean operators u Boolean Operators: u C has no Boolean type it uses int type with 0 for false and nonzero for true a > b > c is a legal expression FORTRAN77FORTRAN90CAda.AND.and&&and.OR.or||or.NOT.not!

14 14 Organization of Programming Languages-Cheng (Fall 2004) Short Circuit Evaluation u Short-circuit evaluation of an expression result is determined without evaluating all operands & operators int a = -1, b = 4; if ((a > 0) && (b < 10)) { … } u Problem: suppose Java did not use short-circuit evaluation index = 1; while (index <= length) && (LIST[index] != value) index++; u C, C++, and Java: use short-circuit evaluation for usual Boolean operators (&& and ||), also provide bitwise Boolean operators that are not short circuit (& and |)

15 15 Organization of Programming Languages-Cheng (Fall 2004) Short Circuit Evaluation u Ada: Non-short-circuit: AND OR short-circuit: AND THENOR ELSE Index = 1; while (Index <= Listlen) and then (List(Index) /= Key) loop Index = Index + 1; end loop; u Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3) (b is changed only when a <= b)

16 16 Organization of Programming Languages-Cheng (Fall 2004) Assignment Statements u The assignment operator symbol: = FORTRAN, BASIC, PL/I, C, C++, Java := ALGOLs, Pascal, Modula-2, Ada u = can be bad if it is overloaded for the relational operator for equality e.g. (PL/I) A = B = C;

17 17 Organization of Programming Languages-Cheng (Fall 2004) Assignment Statements u More complicated assignments: Multiple targets (PL/I)  A, B = 10 Conditional targets (C, C++, and Java)  x = flag ? count1 : count2 = 0; Compound assignment operators (C, C++, and Java)  sum += next; Unary assignment operators (C, C++, and Java)  a++;  - count ++;

18 18 Organization of Programming Languages-Cheng (Fall 2004) Assignment Statements  C, C++, and Java treat = as an arithmetic binary operator e.g. a = b * (c = d * 2 + 1) + 1 This is inherited from ALGOL 68 u Assignment as an Expression In C, C++, and Java, the assignment statement produces a result So, they can be used as operands in expressions  e.g. while ((ch = getchar() != EOF) {... } u Disadvantage Another kind of expression side effect u Exercise: a=1, b=2, c=3, d=4 a = b + (c = d / b++) – 1 cout << a << “,” << b << “,” << c << “,” << d << endl

19 19 Organization of Programming Languages-Cheng (Fall 2004) Mixed Mode Assignment u In FORTRAN, C, and C++ any numeric value can be assigned to any numeric scalar variable; whatever conversion that is necessary is done u In Pascal integers can be assigned to reals, but reals cannot be assigned to integers programmer must specify whether the conversion from real to integer is truncated or rounded u In Java, only widening assignment coercions are done u In Ada, there is no assignment coercion

20 20 Organization of Programming Languages-Cheng (Fall 2004) Control Structures u A control structure is a control statement and the statements whose execution it controls u Types of control statements: Selection statements Iterative statements Unconditional branching statement u Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements

21 21 Organization of Programming Languages-Cheng (Fall 2004) Design Issues u What control statements should a language have? u Can a control structure have multiple entries? Single entry:  execution of the code segment begins with the first statement in the segment Multiple entries are possible in languages that include gotos and statement labels Multiple entries may add flexibility to a control construct u Can a control structure have multiple exits?

22 22 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u Provides the means of choosing between two or more execution paths in a program u Types of Selection Statements: One-way selection statements Two-way selection statements N-way (multiple) selection statements u Nested selection statements?

23 23 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u Single-Way Examples FORTRAN IF: IF (boolean_expr) statement u Problem: can select only a single statement; to select more, a GOTO must be used, as in the following example IF (.NOT. condition) GOTO 20... 20 CONTINUE

24 24 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u Two-way selection statements if control_expression then clause else clause Control_expression  arithmetic/Boolean expressions Clause form  Can be single statements or compound statements (statements in a program block)

25 25 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u Nested Selectors if (sum == 0) if (count == 0) result = 0; else result = 1; u Which if gets the else? Java's static semantics rule: else goes with the nearest if To force alternative semantics, use compound statement if (sum == 0) { if (count == 0) result = 0; } else result = 1;

26 26 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u FORTRAN 90 and Ada solution use special words to resolve semantics of nested selectors u e.g. (Ada) u Advantage: flexibility and readability if.. then if … then … end if else … end if if.. then if … then … else … end if

27 27 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u Multiple Selection Constructs C: switch (expression) { case const_expr_1: statement_1; … case const_expr_k: statement_k; [default:def_statement;] (optional) }

28 28 Organization of Programming Languages-Cheng (Fall 2004) Selection Statements u Early Multiple Selectors: FORTRAN arithmetic IF (a three-way selector) IF (arithmetic expression) N1, N2, N3 u Multiple Selection using if: if Expr1 then statement_1 elsif Expr2 then statement_2 … else statement_k end if;


Download ppt "CSE 452: Programming Languages Expressions and Control Flow."

Similar presentations


Ads by Google