1 / 11

Expressions and assignments

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

jeanne
Télécharger la présentation

Expressions and assignments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Expressions and assignments CS101 2012.1

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

More Related