Operators
Operators
C++ Operators
Example
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and
another variable:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
Arithmetic Operator
These operators operate or work with two operands. C++ provides 5 Binary Arithmetic
Operators for performing arithmetic functions:
Name of the
Operator Operation
Operators Implementation
b = 20;
ch = 'y';
“+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first
adds the current value of the variable on left to the value on the right and
then assigns the result to the variable on the left.
Example:
(a += b) can be written as (a = a + b)
== Is Equal To 3 == 5
!= Not Equal To 3 != 5
== Operator
int x = 10;
int y = 15;
int z = 10;
x == y // false
x == z // true
!= Operator
int x = 10;
int y = 15;
int z = 10;
x != y // true
x != z // false
> Operator
int x = 10;
int y = 15;
x > y // false
y > x // true
< Operator
int x = 10;
int y = 15;
x < y // true
y < x // false
>= Operator
int x = 10;
int y = 15;
int z = 10;
x >= y // false
y >= x // true
z >= x // true
<= Operator
int x = 10;
int y = 15;
x > y // false
y > x // true
Logical AND.
&& expression1 && expression 2
true only if all the operands are true.
Logical OR.
|| expression1 || expression 2
true if at least one of the operands is true.
Logical NOT.
! !expression
true only if the operand is false.
0 0 0
0 1 0
1 0 0
1 1 1
As we can see from the truth table above, the && operator returns true only if
both a and b are true.
Note: The Logical AND operator && should not be confused with the Bitwise AND
operator &.
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 9;
return 0;
}
Run Code
Output
0
0
0
1
Here, a == 0 evaluates to false as the value of a is 5. a > b is also false since the
value of a is less than that of b. We then use the AND operator && to combine these
two expressions.
From the truth table of && operator, we know that false && false (i.e. 0 && 0) results in
an evaluation of false (0). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table of
the && operator.
C++ Logical OR Operator
0 0 0
0 1 1
1 0 1
1 1 1
As we can see from the truth table above, the || operator returns false only if
both a and b are false.
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 9;
return 0;
}
Run Code
Output
0
1
1
1
Here, a == 0 evaluates to false as the value of a is 5. a > b is also false since the
value of a is less than that of b. We then use the OR operator || to combine these two
expressions.
From the truth table of || operator, we know that false || false (i.e. 0 || 0) results in an
evaluation of false (0). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table
of || operator.
The logical NOT operator ! is a unary operator i.e. it takes only one operand.
It returns true when the operand is false, and false when the operand is true.
Truth Table of the ! Operator
Let a be an operand. Then,
int main() {
int a = 5;
// !false = true
cout << !(a == 0) << endl;
// !true = false
cout << !(a == 5) << endl;
return 0;
}
Run Code
Output
1
0
!(a == 0)
Here, a == 0 evaluates to false as the value of a is 5. However, we use the NOT
operator ! on a == 0. Since a == 0 evaluates to false, the ! operator inverts the results
of a == 0 and the final result is true.
Similarly, the expression !(a == 5) ultimately returns false because a == 5 is true.
References: