Download presentation

Presentation is loading. Please wait.

Published byHortense Lang Modified over 4 years ago

1
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions

2
Chapter 3 slide 2 Topics 3.2 Mathematical Expressions 3.3 Implicit Type Conversion 3.4 Explicit Type Conversion 3.5 Overflow and Underflow 3.6 Named Constants 3.7 Multiple Assignment and Combined Assignment

3
Chapter 3 slide 3 3.2 Mathematical Expressions An expression can be a constant, a variable, or a combination of constants and variables combined with operators Can create complex expressions using multiple mathematical operators Example mathematical expressions 2 height a + b / c

4
Chapter 3 slide 4 Using Mathematical Expressions Can be used in assignment statements, with cout, and in other types of statements Examples: area = 2 * PI * radius; cout << "border is: " << 2*(l+w); This is an expression These are expressions

5
Chapter 3 slide 5 Order of Operations In an expression with > 1 operator, evaluate in this order - (unary negation) in order, left to right * / % in order, left to right + - in order, left to right In the expression 2 + 2 * 2 – 2, Do first Do last Do next Evaluate 1st Evaluate 2nd Evaluate 3rd

6
Chapter 3 slide 6 Associativity of Operators - (unary negation) associates right to left * / % + - all associate left to right parentheses ( ) can be used to override the order of operations 2 + 2 * 2 – 2 = 4 (2 + 2) * 2 – 2 = 6 2 + 2 * (2 – 2) = 2 (2 + 2) * (2 – 2) = 0

7
Chapter 3 slide 7 Algebraic Expressions Multiplication requires an operator Area = lw is written as Area = l * w; There is no exponentiation operator Area = s 2 is written as Area = pow(s, 2); Parentheses may be needed to maintain order of operations is written as m = (y2-y1)/(x2-x1);

8
Chapter 3 slide 8 3.3 Implicit Type Conversion Operations are performed between operands of the same type If not of the same type, C++ will automatically convert one to be the type of the other This can impact the results of calculations

9
Chapter 3 slide 9 Hierarchy of Data Types Highest Lowest Ranked by largest number they can hold long double double float unsigned long long unsigned int int

10
Chapter 3 slide 10 Type Coercion Coercion: automatic conversion of an operand to another data type Promotion: converts to a higher type Demotion: converts to a lower type

11
Chapter 3 slide 11 Coercion Rules 1) char, short, unsigned short are automatically promoted to int 2)When operating on values of different data types, the lower one is promoted to the type of the higher one. 3)When using the = operator, the type of expression on right will be converted to type of variable on left

12
Chapter 3 slide 12 3.4 Explicit Type Conversion Also called type casting Used for manual data type conversion Format static_cast (expression) Example: cout (65); // Displays ‘A’

13
Chapter 3 slide 13 More Type Casting Examples char ch = 'C'; cout << ch << " is stored as " (ch); cartons = static_cast (eggs/12); avg = static_cast (sum)/count;

14
Chapter 3 slide 14 3.5 Overflow and Underflow Occurs when assigning a value that is too large (overflow) or too small (underflow) to be held in a variable Variable contains value that is ‘wrapped around’ the set of possible values

15
Chapter 3 slide 15 Overflow Example // Create a short int initialized to // the largest value it can hold short int num = 32767; cout << num; // Displays 32767 num++; cout << num; // Displays -32768

16
Chapter 3 slide 16 Handling Overflow and Underflow Different systems handle the problem differently. They may –display a warning / error message – stop the program – or just continue execution

17
Chapter 3 slide 17 Combined Assignment Applies an arithmetic operation to a variable and assigns the result as the new value of that variable Operators: += -= *= /= %= Example: sum += amt; is “short” for sum = sum + amt;

18
Chapter 3 slide 18 More Examples x += 5; means x = x + 5; x -= 5; means x = x – 5; x *= 5; means x = x * 5; x /= 5; means x = x / 5; x %= 5; means x = x % 5; The entire right hand side is evaluated before the combined assignment operation is done. x *= a + b; means x = x * (a + b);

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google