Expression
Expression
Arithmetic Expressions
- Their evaluation was one of the motivations for the
development of the first programming languages
- Advantage: it works!
- Disadvantage: Programmers want the flexibility
of two-way parameters (what about C?) and
nonlocal references
Operator Overloading
- Some is common (e.g., + for int and float)
Potential problems:
e.g.
Ada:
FLOAT(INDEX) -- INDEX is INTEGER type
C:
(int)speed /* speed is float type */
Errors in Expressions
- Caused by:
Boolean Expressions
- Operands are boolean and the result is boolean
- Operators:
Ada: **
*, /, mod, rem
unary -, not
+ , -, &
relops
and, or, xor
index := 1;
while (index <= length) and
(LIST[index] <> value) do
index := index + 1
Assignment Statements
The operator symbol:
1. = FORTRAN, BASIC, PL/I, C, C++, Java
2. := ALGOLs, Pascal, Modula-2, Ada
e.g. (PL/I) A = B = C;
A, B = 10
sum += next;
e.g.
a = b * (c = d * 2 + 1) + 1
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