







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment
Expressions are the fundamental means of specifying computations in a programming language. To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation. Essence of imperative languages is dominant role of assignment statements.
Their evaluation was one of the motivations for the development of the first programming languages. Most of the characteristics of arithmetic expressions in programming languages were inherited from conventions that had evolved in math. Arithmetic expressions consist of operators, operands, parentheses, and function calls. The operators can be unary , or binary. C-based languages include a ternary operator, which has three operands (conditional expression). The purpose of an arithmetic expression is to specify an arithmetic computation. An implementation of such a computation must cause two actions: o Fetching the operands from memory o Executing the arithmetic operations on those operands. Design issues for arithmetic expressions:
1. Precedence 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). Typical precedence levels: 1. parentheses 2. unary operators 3. ** (if the language supports it) 4. *, /
3. Parentheses Programmers can alter the precedence and associativity rules by placing parentheses in expressions. A parenthesized part of an expression has precedence over its adjacent un-parenthesized parts. Ex:
(A + B) * C
4. Conditional Expressions Sometimes if-then-else statements are used to perform a conditional expression assignment.
if (count == 0) average = 0; else average = sum / count;
In the C-based languages, this can be specified more conveniently in an assignment statement using a conditional expressions. Note that? is used in conditional expression as a ternary operator (3 operands).
expression_1? expression_2 : expression_
Ex:
average = (count == 0)? 0 : sum / count;
The process:
a + fun(a)
If fun does not have the side effect of changing a, then the order of evaluation of the two operands, a and fun(a), has no effect on the value of the expression. However, if fun changes a, there is an effect. Ex: Consider the following situation: fun returns the value of its argument divided by 2 and changes its parameter to have the value 20, and:
a = 10; b = a + fun(a);
If the value of a is returned first (in the expression evaluation process), its value is 10 and the value of the expression is 15. But if the second is evaluated first, then the value of the first operand is 20 and the value of the expression is 25. The following shows a C program which illustrate the same problem.
int a = 5; int fun1() { a = 17; return 3; } void fun2() { a = a + fun1(); // C language a = 20; Java a = 8 } void main() { fun2(); }
The value computed for a in fun2 depends on the order of evaluation of the operands in the expression a + fun1(). The value of a will be either 8 or 20. Two possible solutions:
Java guarantees that operands are evaluated in left-to-right order , eliminating this problem. // C language a = 20; Java a = 8
void mymethod() { int a, b, c; float d; … a = b * d; … }
Assume that the second operand was supposed to be c instead of d. Because mixed-mode expressions are legal in Java, the compiler would not detect this as an error. Simply, b will be coerced to float.
Often called casts in C-based languages. Ex: Ada:
FLOAT(INDEX)--INDEX is INTEGER type
Java:
(int)speed /speed is float type/**
Short Circuit Evaluation
A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators. Ex:
(13 * a) * (b/13 – 1) // is independent of the value (b/13 – 1) if a = 0, because 0*x = 0.
So when a = 0, there is no need to evaluate (b/13 – 1) or perform the second multiplication. However, this shortcut is not easily detected during execution, so it is never taken. The value of the Boolean expression:
(a >= 0) && (b < 10) // is independent of the second expression if a < 0, because(F && x) is False for all the values of x.
So when a < 0, there is no need to evaluate b, the constant 10, the second relational expression, or the && operation. Unlike the case of arithmetic expressions, this shortcut can be easily discovered during execution. Short-circuit evaluation exposes the potential problem of side effects in expressions
(a > b) || (b++ / 3) // b is changed only when a <= b.
If the programmer assumed b would change every time this expression is evaluated during execution, the program will fail. 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 | )
Assignment Statements
The C-based languages use == as the equality relational operator to avoid confusion with their assignment operator. The operator symbol for assignment:
Ex:
flag? count 1 : count2 = 0; ⇔ if (flag) count1 = 0; else count2 = 0;
A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment. The form of assignment that can be abbreviated with this technique has the destination var also appearing as the first operand in the expression on the right side, as in
a = a + b
The syntax of assignment operators that is the catenation of the desired binary operator to the = operator. sum += value; ⇔ sum = sum + value;
C-based languages include two special unary operators that are actually abbreviated assignments. They combine increment and decrement operations with assignments. The operators ++ and -- can be used either in expression or to form stand- alone single-operator assignment statements. They can appear as prefix operators:
sum = ++ count; ⇔ count = count + 1; sum = count;
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. In all languages that allow mixed-mode assignment, the coercion takes place only after the right side expression has been evaluated. For example, consider the following code:
int a, b; float c; … c = a / b;
Because c is float, the values of a and b could be coerced to float before the division, which could produce a different value for c than if the coercion were delayed (for example, if a were 2 and b were 3).