UNIT-II Part 2 Expressions and Statements
UNIT-II Part 2 Expressions and Statements
Def: The operator precedence rules for expression evaluation define the
order in which “adjacent” operators of different precedence levels are
evaluated (“adjacent” means they are separated by at most one operand)
3
- Typical associativity rules:
- Left to right, except **, which is right to left
- Sometimes unary operators associate right to left (e.g., FORTRAN)
- APL is different; all operators have equal precedence and all operators
associate right to left
- The process:
1. Variables: just fetch the value
2. Constants: sometimes a fetch from memory; sometimes the constant is
in the machine language instruction
3. Parenthesized expressions: evaluate all operands and operators first
4. Function references: The case of most interest!
- Order of evaluation is crucial
Functional side effects - when a function changes a two-way parameter or a
nonlocal variable
4
The problem with functional side effects:
- When a function referenced in an expression alters another operand of the
expression
e.g., for a parameter change:
a = 10;
b = a + fun(&a);
/* Assume that fun changes its parameter */
Operator Overloading
- Some is common (e.g., + for int and float)
- Some is potential trouble (e.g., * in C and C++)
- Loss of compiler error detection (omission of an operand should be a
detectable error)
- Can be avoided by introduction of new symbols (e.g., Pascal’s div)
Potential problems:
6
Implicit Type Conversions
7
Explicit Type Conversions
Errors in Expressions
- Caused by:
- Inherent limitations of arithmetic e.g. division by zero
- Limitations of computer arithmetic e.g. overflow
- Such errors are often ignored by the run-time system
8
Relational Expressions
Boolean Expressions
- Operands are boolean and the result is boolean
- Operators:
FORTRAN 77 FORTRAN 90 C Ada
.AND. and && and
.OR. or || or
.NOT. not ! not
xor
- C has no boolean type--it uses int type with 0 for false and nonzero for
true
- One odd characteristic of C’s expressions: a < b < c is a legal
expression, but the result is not what you might expect
9
Precedence of All Operators:
10
C, C++, and Java: use short-circuit evaluation for the usual Boolean
operators (&& and ||), but also provide bitwise Boolean operators
that are not short circuit (& and |)
FORTRAN 77: short circuit, but any side-affected place must be set to
undefined
Assignment Statements
The operator symbol:
1. = FORTRAN, BASIC, PL/I, C, C++, Java
2. := ALGOLs, Pascal, Modula-2, Ada
= can be bad if it is overloaded for the
relational operator for equality
e.g. (PL/I) A = B = C;
Note difference from C
11
More complicated assignments:
1. Multiple targets (PL/I)
A, B = 10
12
Assignment as an Expression
Disadvantage
- Another kind of expression side effect
Mixed-Mode Assignment
- In FORTRAN, C, and C++, any numeric value can be assigned to any
numeric scalar variable; whatever conversion is necessary is done
- In Pascal, integers can be assigned to reals, but reals cannot be assigned to
integers (the programmer must specify whether the conversion from real
to integer is truncated or rounded)
- In Java, only widening assignment coercions are done
- In Ada, there is no assignment coercion
13
Levels of Control Flow:
1. Within expressions
2. Among program units
3. Among program statements
Evolution:
A block is a compound statement that can define a new scope (with local
variables)
Selection Statements
Design Issues:
1. What is the form and type of the control expression?
2. What is the selectable segment form (single statement, statement
sequence, compound statement)?
3. How should the meaning of nested selectors be specified?
Single-Way Examples
Problem: can select only a single statement; to select more, a goto must be
used, as in the following example
15
FORTRAN example:
ALGOL 60 if:
if (boolean_expr) then
begin
...
end
17
FORTRAN 77, Ada, Modula-2 solution - closing
special words
18
Multiple Selection Constructs
Design Issues:
case expression of
constant_list_1 : statement_1;
...
constant_list_n : statement_n
end
Design choices:
1. Expression is any ordinal type (int, boolean, char, enum)
2. Segments can be single or compound
3. Construct is encapsulated
4. Only one segment can be executed per execution of the construct
5. In Wirth's Pascal, result of an unrepresented control expression value is
undefined (In 1984 ISO Standard, it is a runtime error)
- Many dialects now have otherwise or else clause
20
2. The C and C++ switch
switch (expression) {
constant_expression_1 : statement_1;
...
constant_expression_n : statement_n;
[default: statement_n+1]
}
Design Choices: (for switch)
1. Control expression can be only an integer type
2. Selectable segments can be statement sequences, blocks, or compound
statements
3. Construct is encapsulated
4. Any number of segments can be executed in one execution of the
construct (there is no implicit branch at the end of selectable segments)
5. default clause is for unrepresented values (if there is no default, the
whole statement does nothing)
- Design choice 4 is a trade-off between reliability and flexibility
(convenience)
- To avoid it, the programmer must supply a break statement for each
segment
21
3. Ada's case is similar to Pascal's case, except:
1. Constant lists can include:
- Subranges e.g., 10..15
- Boolean OR operators
e.g., 1..5 | 7 | 15..20
2. Lists of constants must be exhaustive
- Often accomplished with others clause
- This makes it more reliable
Multiple Selectors can appear as direct extensions to two-way selectors, using else-if
clauses (ALGOL 68, FORTRAN 77, Modula-2, Ada)
Ada:
if ...
then ...
elsif ...
then ...
elsif ...
then ...
else ...
end if
22
Iterative Statements
- The repeated execution of a statement or compound statement is
accomplished either by iteration or recursion; here we look at iteration,
because recursion is unit-level control
Counter-Controlled Loops
Design Issues:
1. What is the type and scope of the loop var?
2. What is the value of the loop var at loop termination?
3. Should it be legal for the loop var 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?
23
1. FORTRAN 77 and 90
- Design choices:
1. Loop var can be INTEGER, REAL, or DOUBLE
2. Loop var always has its last value
3. The loop var cannot be changed in the loop, but the parameters can;
because they are evaluated only once, it does not affect loop control
4. Loop parameters are evaluated only once
24
2. ALGOL 60
- Syntax: for var := <list_of_stuff> do statement where <list_of_stuff> can
have:
- list of expressions
- expression step expression until expression
- expression while boolean_expression
25
3. Pascal
- Syntax:
for variable := initial (to | downto) final do
statement
- Design Choices:
1. Loop var must be an ordinal type of usual scope
2. After normal termination, loop var is undefined
3. The loop var cannot be changed in the loop; the loop parameters can be
changed, but they are evaluated just once, so it does not affect loop
control
4. Just once
4. Ada
- Syntax:
for var in [reverse] discrete_range loop
...
end loop
26
Ada Design choices:
1. Type of the loop var is that of the discrete range; its scope is the
loop body (it is implicitly declared)
2. The loop var does not exist outside the loop
3. The loop var cannot be changed in the loop, but the discrete range
can; it does not affect loop control
4. The discrete range is evaluated just once
5. C
- Syntax:
for ([expr_1] ; [expr_2] ; [expr_3]) statement
- The expressions can be whole statements, or even statement sequences,
with the statements separated by commas
- The value of a multiple-statement expression is the value of the last
statement in the expression
e.g.,
for (i = 0, j = 10; j == i; i++) ...
27
- If the second expression is absent, it is an infinite
loop
C Design Choices:
1. There is no explicit loop var
2. Irrelevant
3. Everything can be changed in the loop
4. Pretest
5. The first expression is evaluated once, but the other two are evaluated
with each iteration
- This loop statement is the most flexible
6. C++
- Differs from C in two ways:
1. The control expression can also be Boolean
2. The initial expression can include variable definitions (scope is from
the definition to the end of the function in which it is defined)
7. Java
- Differs from C++ in two ways:
1. Control expression must be Boolean
2. Scope of variables defined in the initial expression is only the loop body
28
Logically-Controlled Loops
- Design Issues:
1. Pretest or postest?
2. Should this be a special case of the counting loop statement (or a
separate statement)?
- Language Examples:
1. Pascal has separate pretest and posttest logical loop statements (while-do
and repeat-until)
2. C and C++ also have both, but the control expression for the posttest
version is treated just like in the pretest case (while - do and do - while)
3 Java is like C, except the control expression must be Boolean (and the
body can only be entered at the beginning--Java has no goto)
4. Ada has a pretest version, but no posttest
5. FORTRAN 77 and 90 have neither
29
User-Located Loop Control Mechanisms
- Design issues:
1. Should the conditional be part of the exit?
2. Should the mechanism be allowed in an already controlled loop?
3. Should control be transferable out of more than one loop?
Examples:
1. Ada - conditional or unconditional; for any loop; any number of levels
for ... loop LOOP1:
... while ... loop
exit when ... ...
... LOOP2:
end loop for ... loop
...
exit LOOP1 when ..
...
end loop LOOP2;
...
end loop LOOP1;
30
2. C , C++, and Java - break
Unconditional; for any loop or switch; one level only (Java’s can have a
label)
There is also has a continue statement for loops; it skips the remainder of this
iteration, but does not exit the loop
3. FORTRAN 90 - EXIT
Unconditional; for any loop, any number of levels
FORTRAN 90 also has CYCLE, which has the same semantics as C's
continue
31
- Perl has a built-in iterator for arrays and hashes
- e.g., foreach $name (@names) { print $name }
Unconditional Branching
Problem: readability
- Some languages do not have them:e.g., Modula-2 and Java
Label forms:
1. Unsigned int constants: Pascal (with colon)
FORTRAN (no colon)
2. Identifiers with colons: ALGOL 60, C
3. Identifiers in << ... >>: Ada
4. Variables as labels: PL/I
- Can be assigned values and passed as parameters
- Highly flexible, but make programs impossible to read and difficult to
implement
32
Restrictions on Pascal's gotos:
Idea: if the order of evaluation is not important, the program should not
specify one
34
2. Loops do <boolean> -> <statement>
[] <boolean> -> <statement>
...
[] <boolean> -> <statement>
od
35