lesson - 7. operators there are three types of operators: arithmetic operators relational and...

10
C++ Operator Lesson - 7

Upload: lee-norton

Post on 04-Jan-2016

224 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

C++ Operator

Lesson - 7

Page 2: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

OperatorsThere are three types of operators:

Arithmetic OperatorsRelational and Equality Operators Logical Operators

Page 3: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

Arithmetic OperatorsMain operators used in C++ are:

Operation Operator Example

Addition + 5+4 = 9

Subtraction - 5-4=1 , 4-5=-1

Multiplication * 5*4=20

Integer Division / 15/3=5 , 12/5=2

Modulus % 15%3=0, 12%5=2, 4%5=4

Page 4: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

C++ Relational and Equality OperatorsC++ Relational and Equality Operators are:

Operation Operator Example

Greater > 8>7, b>a, Num1>Num2, Char1>Char2

Greater or Equal >= 15>= 14, c>=b

Smaller < 7<8, d<e

Smaller or Equal <= Num1<=Num2, Char1<=Char2

Equal == Num1==Num2, strg1==strg2

Not Equal != Num1!=Num2, Variable1!=Variable2

Page 5: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

Logical OperatorsLogical operators simplify nested if and

if/else structures. They combine multiple logical expressions

and return a single result (True or False). There are three logical operators in C++:

! (Not) && (And) || (Or)

Page 6: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

! Logical Not Operator! Logical Not Operator has

only one operand (unary operator) and returns the opposite of it.

Not Operator gives true if the operand is false, and gives false if the operand is true.

For example:!(5 > 7) //evaluates to true.!true //evaluates to false.

X !X

0 1

1 0

The truth table of the Not Operator ( ! )

Page 7: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

&& Logical And Operator&& Logical And Operator has two

operands (binary operator). It returns true only if both operands are

true, and returns false otherwise. So we may need this operator when we

have to perform a task if two conditions are fulfilled at the same time.

For example we want to determine if a given integer (num) is divisible by 3 and 7.

if ((num % 3 == 0) && (num % 7 == 0))cout<<"It is divisible by 3 and 7";

X Y X&&Y

0 0 0

0 1 0

1 0 0

1 1 1

The truth table of the And Operator ( && )

Page 8: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

|| Logical Or Operator|| Logical Or Operator has two

operands. It returns false if both operands are

false, and returns true otherwise. It can be used in a case if at least

one of two conditions has to be true to perform a task.

For example we want to check if a number is divisible by 3 or 7.

if ((num % 3 == 0) || (num % 7 == 0))

cout<<"It is divisible by 3 or 7";

X Y X||Y

0 0 0

0 1 1

1 0 1

1 1 1

The truth table of the Or Operator (||)

Page 9: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

Increment and Decrement OperatorsC++ provides unary increment (++) and

decrement (--) operators. Both operators can be used after (a++, post-

increment) or before (++a, pre-increment) their operands.

If the variable 'a' needs to be incremented by 1, "a++", "a=a+1", or "a+=1" can be used.

If the variable 'a' needs to be decremented by 1, "a--", "a=a-1", or "a-=1" can be used.

Page 10: Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

End of Lesson!Thank you for your

listening!