Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary.

Similar presentations


Presentation on theme: "Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary."— Presentation transcript:

1

2

3 Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary

4 Arithmetic Expressions Algebraic Exp. C++ Expression f + 7 p - c bx r mod s f + 7 p - c b * x x / y r % s x/y or x y

5 RegWages ?.? OTWages ?.? OTHours 10 RegHours 40.0 OTPay 27.78 BasePay 18.25 TotalWages ?.? #include #include void main () { double RegWages, BasePay = 18.25; double RegHours = 40.0; double RegHours = 40.0; double OTWages, OTPay = 27.78; double OTWages, OTPay = 27.78; double OTHours = 10; double OTHours = 10; double TotalWages; double TotalWages;

6 Basic usage of operators RegWages ?.? OTWages ?.? OTHours 10 RegHours 40.0 OTPay 27.78 BasePay 18.25 TotalWages ?.? 277.8 1007.8 RegWages = BasePay * RegHours; RegWages = BasePay * RegHours; OTWages = OTPay * OTHours; OTWages = OTPay * OTHours; TotalWages = RegWages + OTWages; TotalWages = RegWages + OTWages; cout << “Wages for this week are $ ” cout << “Wages for this week are $ ” << TotalWages ; << TotalWages ; } 730.0

7 Precedence of Operations Operator (s) Operation(s) ( ) *, /, or % + or - Parentheses Multiplication Division Modulus Addition Subtraction

8 How precedence works x = ( 3 + 8 ) / 3 * 2 + 5 % 3 x = ? / 3 x =3* 2 * 2 + 5 % 3 + 5 % 3 x =+ 5 % 36 x = 6+2 8 11

9 Compound Operators += - =- =- =- = *= %= x = x + 5; y = y - 2; z = z * 10; a = a / b c = c % 3; /= x += 5; y - = 2; z *= 10; c %= 3; a /= b;

10 The Typecast Operator #Include void main () {int Months, Books; double PerMonth; cout << “How many books do” <<“you plan to read?”; cin >> Books; cout << “ How many months will” <<“it take you to read them?”; cin >> Months; PerMonth = static_cast (Books) / Months; cout << “That is” << PerMonth” <<“books per month.\n; }

11 The Typecast Operator #Include void main () {int Months, Books; double PerMonth; cout << “How many books do” << “you plan to read?”; cin >> Books; cout << “ How many months will” << “it take you to read them?”; cin >> Months; PerMonth = static_cast (Books) / Months; cout << “That is” << PerMonth << “books per month.\n”; }

12 The Typecast Operator #include void main () { int Number = 65; cout << Number << endl; // C method of type casting cout << char(Number) << endl; }

13 cout << (a + 5); Expressions with a value

14 Statement Screen Output cout << (a + b);11 cout << (b * 2);14 cout << (b + (a*2));15 cout << (a = 6);6 Expressions with a value Assume a is 4 and b is 7

15 Duo ? 5 10 Unus ? 59 Tres ? 5 11 3 1 3 _ 11 _ 1 _ # include void main () { int Unus, Duo, Tres; Unus = Duo = Tres = 5; Unus = Duo = Tres = 5; Unus += 4; Unus += 4; Duo *= 2; Duo *= 2; Tres -= 4; Tres -= 4; Unus /= 3; Unus /= 3; Duo += Tres; Duo += Tres; cout << Unus << endl; cout << Unus << endl; cout << Duo << endl; cout << Duo << endl; cout << Tres << endl; cout << Tres << endl; }

16 Unusual ? Strange ? Mystery ? 102 5 2 _ 5 _ 10 _ # include # include void main () void main () { int Unusual, Mystery, Strange; cout << (Unusual = 2) << endl; cout << (Unusual = 2) << endl; cout << (Strange = Unusual + 3) << endl; cout << (Strange = Unusual + 3) << endl; cout << (Mystery = Strange * 2) cout << (Mystery = Strange * 2) << endl; << endl;

17 Unusual ? Strange ? Mystery ? 102 5 2 _ 5 _ 10 _ 2 _ 5 _ 10 _ cout << Unusual << endl; cout << Unusual << endl; cout << Strange << endl; cout << Strange << endl; cout << Mystery << endl; cout << Mystery << endl; }

18 What is this program’s output #include #include void main () { int X = 0, Y = 2; X = Y * 4; X = Y * 4; cout << X << endl << Y << endl; cout << X << endl << Y << endl; } 8 2 _

19 Write assignment statements A) Adds 2 to A and stores the result in B. B = A + 2

20 Write assignment statements B) Multiplies B times 4 and stores the result in A. A = B * 4;

21 Write assignment statements C) Divides A by 3.14 and stores the result in B. B = A / 3.14;

22 Write assignment statements D) Subtracts 8 from B and stores the result in A. A = B - 8;

23 What is this program’s output void main () { int A, X = 23; int A, X = 23; A = X % 2; A = X % 2; cout << X << endl << A << endl; cout << X << endl << A << endl; } 23 1 _


Download ppt "Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary."

Similar presentations


Ads by Google