Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary
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
RegWages ?.? OTWages ?.? OTHours 10 RegHours 40.0 OTPay BasePay 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;
Basic usage of operators RegWages ?.? OTWages ?.? OTHours 10 RegHours 40.0 OTPay BasePay TotalWages ?.? 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
Precedence of Operations Operator (s) Operation(s) ( ) *, /, or % + or - Parentheses Multiplication Division Modulus Addition Subtraction
How precedence works x = ( ) / 3 * % 3 x = ? / 3 x =3* 2 * % % 3 x =+ 5 % 36 x =
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;
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; }
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”; }
The Typecast Operator #include void main () { int Number = 65; cout << Number << endl; // C method of type casting cout << char(Number) << endl; }
cout << (a + 5); Expressions with a value
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
Duo ? 5 10 Unus ? 59 Tres ? _ 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; }
Unusual ? Strange ? Mystery ? _ 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;
Unusual ? Strange ? Mystery ? _ 5 _ 10 _ 2 _ 5 _ 10 _ cout << Unusual << endl; cout << Unusual << endl; cout << Strange << endl; cout << Strange << endl; cout << Mystery << endl; cout << Mystery << endl; }
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 _
Write assignment statements A) Adds 2 to A and stores the result in B. B = A + 2
Write assignment statements B) Multiplies B times 4 and stores the result in A. A = B * 4;
Write assignment statements C) Divides A by 3.14 and stores the result in B. B = A / 3.14;
Write assignment statements D) Subtracts 8 from B and stores the result in A. A = B - 8;
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 _