Statements (Expressions and Assignment)
Arithmetic Expressions
- consist of operators, operands and parentheses
Operators
can be infix, prefix, or postfix (most of them are infix)
Unary Operator: has one operator
i++,--i
Binary Operator: has two operators
a + b
Ternary Operator: has three operators
a ? b : c
Operator Precedence Rules
- determines the order in which operators are evaluated in an expression
- Typical precedence levels:
- Parentheses
() - Exponentiation
**or^ - Multiplication
*,/ - Addition & Subtraction
+&-
- Parentheses
Operator Associativity Rule
- refers to the order in which operators with the same precedence are evaluated in an expression
- Typical associativity rules:
- right to left (except exponentiation
**)1
2 ** 2 ** 3 # 256
- right to left (except exponentiation
- Remind: APL is different: all operators have equal precedence and all operators associate right to left
1
2 * 5 + 7 ⍝ 24
Side Effecr
it occurs when a function changes a two-way parameter, a non-local variable, or a static-storage variable
updated a non-local variable
1
2
3
4
5
6
7
8
9
10
11
12int fun(int& x){
x += 1;
return x * 2;
}
int main(){
int a = 0;
cout << a + fun(a) << endl; // 3 (expected: 2)
int b = 0;
cout << b + 1 + fun(b) << endl; // 3 (the expect value is not previous + 1)
return 0;
}updated a static-storage variable
1
2
3
4
5
6
7
8
9
10
11
12int g = 1;
int fun(int b){
g += 1;
return 2 * b * g;
}
int main(){
cout << fun(2) << endl; // 8
cout << fun(2) << endl; // 12 (the expect value is not same as previous)
return 0;
}
Solutions
- disallow functional side effects
- No two-way parameters in functions
- No static-storage / non-local assignable variable access in function
- Disadvantages: Inflexibility
- disallow functional side effects
Referential Transparency
if it always produces the same result for a given set of input parameters, and its result does not depend on any external state or side effects.
- the function is referential transparency if
result1andresult2are equivalent1
2
3result1 = (fun(a) + b) / (fun(a) - c)
temp = fun(a)
result2 = (temp + b) / (temp - c)
- the function is referential transparency if
Advantages
- much easier to understand as they do not have variables
Overloaded Operators
an operator can work for more than one data type or class
1 + 2: work for int1.1 + 2.2: also work for float&a: getting the address of aa&b: logic and operation
C++,C#, andF#allow user-defined overloaded operators- When sensibly used, such operators can be an aid to readability
Potential problems:
- users can define nonsense operations
readability may suffer, even when the operators make sense
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using std::cout;
using std::endl;
using std::ostream;
class Vector{
public:
Vector(float x, float y, float z) : x(x), y(y), z(z) { }
Vector operator+(const Vector& another){
return Vector(x+another.x, y+another.y, z+another.z);
}
float getX() { return x; }
float getY() { return y; }
float getZ() { return z; }
private:
float x, y, z;
};
ostream& operator<<(ostream& os, Vector v){
os << "(" << v.getX() << "," << v.getY() << "," << v.getZ() << ")";
return os;
}
int main(){
Vector a(1,2,3), b(2,4,6);
Vector c = a + b;
cout << a << " + " << b << " = " << c << endl; return 0;
}
Type Conversions
narrowing conversion (Not allow in
Java)- the converted object cannot include all of the values of the original type by the programming language
floattoint
widening conversion
- the converted object can include at least approximations to all of the values of the original type
inttofloat
Implicit Type Conversions
- automatic conversion of a value from one data type to another
1
2
3
4a = 42
b = 3.14
c = a + b
print(c) # 45.14 (a is automatically converted to float) - Advantage: convenience
- Disadvantage: reliability (lower the error detection ability of the compiler)
Explicit Type Conversions
- manual conversion of a value from one data type to another by the programmer
int(variable)or(int)variable: depend on language1
2
3a = "42"
b = float(a)
print(b) # 42.01
2
3double c = 3.14;
int d = (int) c;
System.out.println(d); // 3
Relational and Boolean Expressions
Relational Expressions
- the comparation of two operands with one relational operator and evaluate to
Boolean - some operator symbols which vary among languages (
!=,ne,<>) JavaScriptandPHPhave two additional relational operator===and!==1
2
3
4
5
61 == 1.0 // true
1 == '1' // true
1 == true // true
1 === 1.0 // true (javascript only has number type)
1 === '1' // false
1 === true // false
Boolean Expressions
The operands and the result are all
BooleanC,C++,Java:&&,||Python:and,or
Traditional C: Boolean
truevalue is1whereas Booleanfalsevalue is0Chain comparison:
1
bool re = 3 > 2 > 1; // false
1
re = 3 > 2 > 1 # True
Short-Circuit Evaluation
an expression in which the result is determined without evaluating all of the operands and/or operators
(87 * a) * (b / 17 - 9): ifais0, then no need to evaluate(b / 17 - 9)A && B: ifAisfalse, then no need to evaluateBA || B: ifAistrue, then no need to evaluateB
bitwise Boolean operators that are not short-circuit:
&and|Advantages: efficiency (save the compilation power)
Condition: program correctness
- if we use the
&operator that doest not has short-circult1
2
3
4
5
6
7
8public static void main(String[] args){
int[] a = {1,2,3};
int index = 0;
while(index < a.length & a[index] != 1337){
index++;
}
System.out.println("test");
} - it is compilation error, because when
indexis 4,a[index]does not exist
- if we use the
Potential error:
(a > b) || (b++ / 5)if(a > b)istrue,b++does not be executed
Assignment Statements
- General Syntax:
<target_variable> <assignment_operator> <expression> Assignment Operators
- Most of languages:
= - Pascal and Ada:
:=
- Most of languages:
Conditional Targets
1
2
3
4
5
6
7
8
9($dog_lover ? $dog_is_da_best : $cat_is_da_best) = 1
# is equivalent to
if ($dog_lover) {
$dog_is_da_best = 1
} else {
$cat_is_da_best = 1
}Compound Assignment Operators
- a shorthand method of specifying a commonly needed form of assignment
a = a + bcan be simply written asa += b
- a shorthand method of specifying a commonly needed form of assignment
Multiple Assignments
- Python, Perl, and Ruby allow multiple-target multiple-source assignments
a, b, c = 1, 2, 3: assign1,2,3toa,b,ca, b = b, a: swapa,b
- Python, Perl, and Ruby allow multiple-target multiple-source assignments
Mixed-Mode Assignment
- allows values of different types to be assigned to each other
1
2
3
4int x = 5;
double y = 3.14;
x = y; // y is implicitly converted to int, x = 3
y = x; // x is implicitly converted to double, y = 5.0
- allows values of different types to be assigned to each other


