In C++ Operators can be of many types some commonly used operators are given below
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations
- + (Addition or Unary plus)
- - (Subtraction or Unary minus)
- * (Multiplication)
- / (Division)
- % (Modulo Division)
Ex: If a and b are integers, then for a=24 and b=9 the results will be:
a+b=33
a-b= 15
a*b=216
a/b=2
a%b= 6
Relational Operators
These operators are used to compare two values and the result given by relational expression is either true or false.
- < (Less Than)
- <= (Less Than or Equal to)
- > (Greater Than)
- >= (Greater Than or Equal to)
- == (Equal to)
- != (Not equal to)
Ex: 10>5 TRUE, 10<=10 TRUE, 3<1 FALSE etc.
Relational expressions are used in decision making statements such as if and while.
Logical Operators
Logical operators used to add two or more relational expression together to construct more complex decision making expression. There are three logical operators:
- && (Logical AND): exp1 && exp2 - This will return true iff both expressions will be true.
- || (Logical OR): exp1 || exp2 - This will return true either exp1 or exp2 will be true.
- ! (Logical NOT): !exp -
Reverse the result, returns false if the result is true.
These are too used in decision statements.
Assignment Operators
C++ offers "=" as assignment operator to assign a value to an identifier. C++ also offers some shorthand assignment operators to simplify the assignment statement.
- += :- Ex- a+=b; (This is equivalent to a=a+b)
- -= :- Ex- a-=b; (This is equivalent to a=a-b)
- *= :- Ex- a*=b; (This is equivalent to a=a*b)
- /= :- Ex- a/=b; (This is equivalent to a=a/b)
- %= :- Ex- a%=b; (This is equivalent to a=a%b)
Increment and Decrement Operator
The increment operator (++) adds 1 to its operand and decrement operator (--) subtract 1 from its operand. In other words
a=a+1 is same as ++a or a++
a=a-1; is same as --a or a--
Pre-Increment (Prefix ++) and Post-Increment (Postfix ++)
In the Pre-Increment, value is first incremented and then used inside
the expression. Whereas in the Post-Increment, value is first used
inside the expression and then incremented.
Ex: The value of b if a=10.
Postfix- b=a++; //b=10
Prefix- b=++a; //b=11
Pre-Decrement (Prefix --) and Post-Decrement (Postfix --)
In the Pre-Decrement, value is first decremented and then used inside
the expression. Whereas in the Post-Decrement, value is first used
inside the expression and then decremented.
Ex: The value of b if a=10.
Postfix- b=a--; //b=10
Prefix- b=--a; //b=9 Conditional Operator or Ternary Operator
The form of conditional operator is:
exp1 ? exp2 : exp3
Where exp1 is logical condition which is either true or false. If exp1 returns true, then the value of whole expression is the value of exp2. Otherwise, the value of whole expression is the value of exp3.
For Ex: greater=a>b?a:b (where a and b are integer variables)
Here, if exp1 i.e. a>b is true the value of greater is a (greater=a) otherwise the value of greater is b (greater=b).