06 Expressions and Assignment Statements
06 Expressions and Assignment Statements
Operator Overloading
· An operator can be used for more than one (1) purpose. This multiple use of an operator is called
operator overloading. Example: x = &y;
The ampersand in C++ is usually used to specify a logical AND operation; but in this case, it was used
to place the address of y in x.
Type Conversions
· Two (2) types of type conversion:
o A narrowing conversion converts a value to a type that cannot store even approximations of
all the values of the original type. Example: Converting a double to a float in Java
o A widening conversion converts a value to a type that can include at least approximations of
the values of the original type. Example: Converting an int to a float in Java
· An expression with operands that have different data types is called a mixed-mode expression.
· A cast is an explicit type conversion, which is performed by placing the desired type in parentheses
just before the expression to be converted. Example: (int) angle
· An error that occurred when the result of an operation is too large is called overflow. An error that
occurred when the result of an operation is too small is called underflow.
Short-Circuit Evaluation
· A short-circuit evaluation of an expression is one in which the result is determine without evaluating
all of the operands and/or operators. Example: (13 * a) * (b / 13 – 1)
Assignment Statements
· An assignment statement stores a value or the result of an expression in the variable.
· A compound assignment operator is a shorthand method of specifying commonly needed form of
assignment. Example: sum += value;
· Prefix operators are operators that precede the operands. Example: sum = ++count;
· Postfix operators are operators that follow the operands. Example: sum = count++;
· An assignment can be used as an expression and as an operand in other expressions.
· Several recent programming languages provide multiple-target, multiple-source assignment
statements. Example: ($first, $second, $third) = (20, 40, 60);
· An assignment that consists of different data types is called a mixed-mode assignment.
References:
Sebesta, Robert W. (2012). Concepts of Programming Languages. 10th ed. USA: Pearson Education, Inc.
Ben-Ari, Mordechai (2006). Understanding Programming Languages. Chichester: John Wiley & Sons, Inc.
Tucker, Allan B. and Noonan, Robert E. (2002). Programming Languages: Principles and Paradigms. 2nd ed.
New York: Mc-Graw Hill