Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators."— Presentation transcript:

1 Lesson - 7

2 Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators

3 Arithmetic Operators Main operators used in C++ are: OperationOperatorExample 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

4 C++ Relational and Equality Operators C++ Relational and Equality Operators are: OperationOperatorExample 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

5 Logical Operators Logical 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)

6 ! 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 01 10 The truth table of the Not Operator ( ! )

7 && 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"; XYX&&Y 000 010 100 111 The truth table of the And Operator ( && )

8 || 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"; XYX||Y 000 011 101 111 The truth table of the Or Operator (||)

9 Increment and Decrement Operators C++ 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.

10 Thank you for your listening!


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

Similar presentations


Ads by Google