lesson - 7. operators there are three types of operators: arithmetic operators relational and...
TRANSCRIPT
C++ Operator
Lesson - 7
OperatorsThere are three types of operators:
Arithmetic OperatorsRelational 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
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
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)
! 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 ( ! )
&& 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 ( && )
|| 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 (||)
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.
End of Lesson!Thank you for your
listening!