In C++, an operator is a symbol used to perform logical and mathematical operations in a C++ program. An expression ia a statement containing operators and variables. C++ operators connects constants and variables to form expressions.
For Example:3.141 x radius x radius;
- x is operator
- 3.141 is a constant
- radius is a variables
- "3.141 x radius x radius;" is an expression, performing one logical task.
C++ Assignment Operators
Assignment Operator in C++ is used to assign a value to a variable. "=" is called simple assignment operator in C++, it assigns values from right side operands(R value) to left side operand (L value).
syntax of assignment operatorage = 25; count = 500;
C++ also supports compound assignment operators. These operators are combination of arithmetic and assignment operators. They first perform arithmetic operation between left and right operand and then assign the result to left operand.
- var+=10; is equivalent to var = var+10;
It first add 10 to var, and then assign result to var.
Operator | Example | Equivalent Expression | Description |
---|---|---|---|
+= | A += B; | A = A+B; | It Adds A and B, then assign result to A |
-= | A -= B; | A = A-B; | It subtract B from A, then assign result to A |
/= | A /= B; | A = A/B; | It divides A by B, then assign result to A |
*= | A *= B; | A = A*B; | It multiply A and B, then assign result to A |
%= | A %= B; | A = A%B; | It takes modulus of A by B, then assign result A |
&= | A &= B; | A = A&B; | It does bitwise AND between A and B, then assign result to A |
|= | A |= B; | A = A|B; | It does bitwise OR between A and B, then assign result to A |
^= | A ^= B; | A = A^B; | It does bitwise XOR between A and B, then assign result to A |
>>= | A >>= B; | A = A>>B; | It does bitwise right shift, then assign result to A |
<<= | A <<= B; | A = A< | It does bitwise left shift, then assign result to A |
C++ Arithmetic Operators
Arithmetic operators in C++ are used to perform mathematical operations. There are five fundamental arithmetic operators supported by C++ language, which are addition(+), subtraction(-), division(/), multiplication(-), and modulus(%) of two numbers.
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Adds two numbers | a + b | 15 + 5 = 20 |
- | Subtracts two numbers | a - b | 15 - 5 = 10 |
* | Multiplies two numbers | a * b | 15 * 5 = 75 |
/ | Divides numerator by denominator | a / b | 15 / 5 = 3 |
% | Returns remainder after an integer division | a % b | 15 % 5 = 0 |
C++ Relational Operators
Relational Operators in C++ are used to compare two values. It specifies the relation between two values like equal, greater than, less than etc. Relational operators always returns boolean value (zero for false and non-zero value for true).
For Example :
(X < Y) : It checks whether X is less than Y or not. It will return none-zero(true) if X is less than Y otherwise zero(false).
Relational Operator | Example | Description |
---|---|---|
> | A > B | Checks if A is greater than B |
< | A < B | Checks if A is less than B |
>= | A >= B | Checks if A is greater than or equal to B |
<= | A <= B | Checks if A is less than or equal to B |
== | A == B | Checks if A is equal to B |
!= | A != B | Checks if A is not equal to B |
C++ Logical Operators
Logical Operators in C++ combines multiple boolean expressions. If we want to combine multiple boolean statements, then we need logical Operators. Logical Operators in C++ always produces boolean results, either TRUE(non-zero value) or FALSE(zero value). There are 3 logical operators in C++ language AND(&&), OR(||) and NOT(!).
- AND : Returns true, If both operands are true otherwise false.
- OR : Returns false If both operands are false, otherwise true.
- NOT : Returns true if operand is false and Returns false if operand is true.
A | B | A && B | A || B | !A |
---|---|---|---|---|
TRUE | FALSE | FALSE | TRUE | FALSE |
TRUE | TRUE | TRUE | TRUE | FALSE |
FALSE | TRUE | FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE | FALSE | TRUE |
C++ Bitwise operators
C++ inherits the mid-level language features from C. It support many operations which can be performed in assembly language like operations on bits. Bitwise operators performs bit-by-bit operations on operands. There are six bitwise operators supported by C++.
Bitwise Operator | Syntax | Description |
---|---|---|
| | A | B | Bitwise OR Operator |
& | A & B | Bitwise AND Operator |
~ | ~A | NOT Operator(One's Complement) |
^ | A ^ B | Bitwise Exclusive OR Operator |
>> | A >> B | Right Shift Operator |
<< | A << B | Left Shift Operator |
C++ Conditional Operator
C++ Conditional operators are also called as Ternary Operator. Conditional Operator is a powerful Operator which can be used to implement if-then-else type of logic.
Syntax of Ternary OperatorTernary Operator will execute Expression_One if Conditional_Expression is true, otherwise it execute Expression_Two.
For Example int X = 25;int Y = (X > 20 ? 1 : 2);
As X > 20, So after above statement Y's value becomes 1.