110 likes | 244 Views
Expressions and assignments. CS101 2012.1. Statement. A typical simple statement assigns a variable the value of an expression So far we have studied numeric types These can be combined into arithmetic and Boolean expressions. Arithmetic expressions. 5 * (fahrenheit – 32) / 9
E N D
Expressions and assignments CS101 2012.1
Statement • A typical simple statement assigns a variable the value of an expression • So far we have studied numeric types • These can be combined into arithmetic and Boolean expressions Chakrabarti
Arithmetic expressions • 5 * (fahrenheit – 32) / 9 • 5 * fahrenheit – 32/9 • (- b + sqrt(b*b – 4*a*c)) / (2*a) • sqrt is a library function like main we already saw • Operator precedence and expression trees / / + * sqrt * 9 a 2 / * 5 b * * 32 9 5 c * b b 32 fahrenheit fahrenheit a 4 Chakrabarti
Logical expressions • Compare numeric expressions • 5 < 13, 1e5 >= 2e6 • a == b+1 (note the double equals), c != d • Each expression is true or false • Internally represented as integers 1 and 0 • Combine using and, or, not • (a == b+1) && (c != d) || !(e <= f) • Operator precedence like with numbers • Logical expressions used to control the execution of statements (soon) Chakrabarti
Assigning values to variables • Lhs = rhs • Lhs is a variable name • Later we will consider arrays, pointers etc. • Rhs is an expression compatible with the type of the lhs • centigrade = 5*(fahrenheit – 32)/9; • Assignment statement has value = rhs • Lets us cascade x = y = z+1; • Some legacy abuse allowed • x = y > z; (what does this mean?) Chakrabarti
Example: swapping two numbers Can you swap without using a temporary variable? float x = 5, y = 11; float temporary = x; x = y; y = temporary; Chakrabarti
Compound assignment /* read two numbers from cin, print sum of their squares */ int sum = 0, num; cin >> num; sum += (num * num); cin >> num; sum += (num * num); cout << sum << endl; How about int va = 5; int vb = (va += 2); Aka “expression with side effect” Chakrabarti
Increment/decrement • Special case of compound assignment • Syntax: ++va and vb++ • ++va means increase va by one and then access the incremented value • vb++ means access the current value of vb and then increment it before any further access • May be slightly inefficient because the old value must be remembered • vb = 2 * (va++); • Similarly va-- and --vb Chakrabarti
Conditional expressions • condition ? valueIfTrue : valueIfFalse • Example: Maximum of a and b • (a > b)? a : b • a/b rounded up to the next integer in case b does not divide a evenly • (a % b)? (a/b + 1) : (a/b) • (a % b > 0)? (a/b + 1) : (a/b) • Note, lazy evaluation • Avoid “side effect” • (a % b)? (c++) : (d--) Chakrabarti
The bool type • Old C++ used int to store Boolean values • But ANSI standard C++ does offer a type called bool • bool tval = true, fval = false; • int ival = int(tval); • However, old bad habits still allowed • if (37) { … } • bool bval = 37; • Overall value unclear Chakrabarti
A dangerous legacy • Boolean conditions and integer values are not strictly separated • 0 is false, everything else is true • Legal, but terrible, to sayif (5) { … } • Avoid this “feature” like the plague • Especially troublesome in case of wrong precedence or bracketing in a if/while condition expression Chakrabarti