22
A R ITH M E TIC O P E R A TO R S & ARITHM ETIC EXPRESSIONS

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

Embed Size (px)

Citation preview

ARITHMETIC OPERATORS &

ARITHMETIC EXPRESSIONS

Arithmetic OperatorsMeaningMeaningOperatorOperator

AdditionAddition

SubtractionSubtraction

MultiplicationMultiplication

DivisionDivision

ModulusModulus

++

--

**

//

%%

TypeType

BinaryBinary

BinaryBinary

BinaryBinary

BinaryBinary

BinaryBinary

Arithmetic ExpressionsAlgebraic Exp.Algebraic Exp. C++ ExpressionC++ 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.0OTPay

27.78

BasePay

18.25

TotalWages

?.?

#include <iostream.h>#include <iostream.h>

void main ()void main (){ double RegWages, BasePay = 18.25;{ 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

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

Precedence of OperationsOperatorOperator(s)(s) Operation(s)Operation(s)

( )

* , / , or %

+ or -

Parentheses

MultiplicationDivisionModulus

AdditionSubtraction

How precedence works

x = ( 3 + 8 ) / 3 * 2 + 5 % 3

x = ? / 3

x = 3 * 2

* 2 + 5 % 3

+ 5 % 3

x = + 5 % 36

x =

x =

6 + 2

8

11

Compound Operators

+=+=-- = =

*=*=

%=%=

x = x + 5;x = x + 5;

y = y - 2;y = y - 2;

z = z * 10;z = z * 10;

a = a / ba = a / b

c = c % 3;c = c % 3;

/= /=

x += 5;x += 5;

y y -- = 2; = 2;

z *= 10;z *= 10;

c %= 3;c %= 3;

a /= b; a /= b;

The Typecast Operator#Include <iostream.h>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 <double> (Books) / Months;cout << “That is” << PerMonth” <<“books per month.\n;

}

The Typecast Operator#Include <iostream.h>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 <double> (Books) / Months;cout << “That is” << PerMonth << “books per month.\n”;

}

The Typecast Operator

#include <iostream.h>void main (){

int Number = 65;cout << Number << endl;

// C method of type castingcout << char(Number) << endl;

}

cout << (a + 5);

Expressions with a value

Statement Screen Outputcout << (a + b); 11cout << (b * 2); 14cout << (b + (a*2)); 15cout << (a = 6); 6

Expressions with a value

Assume a is 4 and b is 7

Duo

?510

Unus

?59

Tres

?511

3

1

3_11_1_

# include <iostream.h># include <iostream.h>void main ()void main (){ int Unus, Duo, Tres;{ 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

?102 5

2 _5 _10 _

# include <iostream.h># include <iostream.h>void main ()void main () { int Unusual, Mystery, Strange;{ 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

?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; }}

What is this program’s output#include <iostream.h> #include <iostream.h>

void main ()void main (){ int X = 0, Y = 2;{ int X = 0, Y = 2; X = Y * 4;X = Y * 4; cout << X << endl << Y << endl;cout << X << endl << Y << endl; }}

82_

Write assignment statements

A) AddsA) Adds 2 2 to to A A and stores the and stores the result inresult in B. B.

B = A + 2B = A + 2

Write assignment statements

B) MultipliesB) Multiplies B B times times 4 4 and and stores the result instores the result in A A..

A = B * 4;A = B * 4;

Write assignment statements

C) DividesC) Divides A A byby 3.14 3.14 and stores and stores the result inthe result in B B..

B = A / 3.14;B = A / 3.14;

Write assignment statements

D) SubtractsD) Subtracts 8 8 fromfrom B B and and stores the result instores the result in A. A.

A = B - 8;A = B - 8;

What is this program’s output void main ()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; }}

231_