C++ Operators
An operator is a symbol which tells compiler to take an action on operands and yield a value. The Value on which operator operates is called as operands. C++ supports wide verity of operators. Supported operators are listed below -
Operator | Explanation |
---|---|
Arithmetic Operators | Used for arithmetic operations |
Relational Operators | Used for specifying relation between two operands |
Logical Operators | Used for identifying the truth value of the expression |
Bitwise Operators | Used for shifting the bits |
Assignment Operators | Used for assigning the value to the variable |
Misc Operators | - |
Arithmetic Operators :
Consider that we have A = 20 and B = 10
Operator | Description | Example |
---|---|---|
+ | Adds two operands or variables | A + B = 30 |
- | Subtracts second operand from the first | A - B = 10 |
* | Multiplies both operands | A * B = 200 |
/ | Divides numerator by denominator | A / B = 2 |
% | After dividing the numberator by denominator remainder will be returned after division | A % B = 0 |
++ | Increment operator will increases integer value by one | A++ = 21 |
#8211; - | Decrement operator will decreases integer value by one | A- #8211; = 19 |
Relational Operators:
Consider that A = 40 and B = 20
Symbol | Meaning | Example |
---|---|---|
> | Greater than | A > B returns true |
< | Less than | A < B returns false |
>= | Greater than equal to | A >= B returns false |
<= | Less than equal to | A <= B returns false |
== | Equal to | A == B returns false |
!= | Not equal to | A != B returns true |
Logical Operators :
Consider that, A = 0 and B = 0
Operator | Description | Example |
---|---|---|
Logical AND (&&) | If both the operands are non-zero then only condition becomes true | (A && B) is false. |
Logical OR (||) | If both the operands are zero then only condition becomes false | (A || B) is true. |
Logical NOT (!) | It will reverses the state of its operand i.e true will become false | (!A) is true. |