C++ Relational and Logical Operators
In this tutorial, we will learn about relational and logical operators with the help of examples.
Relational and Logical Operators
In C++, relational and logical operators compare two or more operands and return either true
or false
values.
We use these operators in decision making.
C++ Relational Operators
- A relational operator is used to check the relationship between two operands.
- Relational operator is alson known as Comparison operator.
- The result of Relational operation is a boolean value that can only be True 1 or False 0.
For example,
// checks if a is greater than c
a > c;
Here, >
is a relational operator. It checks if
a is greater than c or not.
If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
Here, list of relational operator
Operator | Meaning | Example |
---|---|---|
== |
Is Equal To | 2 == 4 gives us false |
!= |
Not Equal To | 2 != 4 gives us true |
> |
Greater Than | 2 > 4 gives us false
|
< |
Less Than | 2 < 4 gives us true
|
>= |
Greater Than or Equal To | 2 >= 4 give us false
|
<= |
Less Than or Equal To | 2 <= 4 gives us true
|
== Operator
The equal to ==
operator returns
true
- if both the operands are equal or the samefalse
- if the operands are unequal
For example,
int x = 5;
int y = 5;
int z = 8;
x == y // true
x == z // false
Note: The relational operator
==
is not the same as the assignment operator =
.
The assignment operator =
assigns a value to a variable,
constant, array, or vector. It does not compare two operands.
!= Operator
The not equal to !=
operator returns
true
- if both operands are unequalfalse
- if both operands are equal.
For example,
int x = 5;
int y = 5;
int z = 6;
x != y // false
x != z // true
> Operator
The greater than >
operator returns
true
- if the left operand is greater than the rightfalse
- if the left operand is less than the right
For example,
int x = 5;
int y = 6;
x > y // false
y > x // true
< Operator
The less than operator <
returns
true
- if the left operand is less than the rightfalse
- if the left operand is greater than right
For example,
int x = 5;
int y = 6;
x < y // true
y < x // false
>= Operator
The greater than or equal to >=
operator returns
true
- if the left operand is either greater than or equal to the rightfalse
- if the left operand is less than the right
For example,
int x = 5;
int y = 7;
int z = 5;
x >= y // false
y >= x // true
z >= x // true
<= Operator
The less than or equal to operator <=
returns
true
- if the left operand is either less than or equal to the rightfalse
- if the left operand is greater than right
For example,
int x = 5;
int y = 7;
x > y // false
y > x // true
In order to learn how relational operators can be used with strings, refer to our tutorial here.
Example 1: C++ Program for Relational Operators
// Program to understand Relational Operator
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 2;
b = 4;
bool result;
result = (a == b); // false
cout << "2 == 4 is " << result << endl;
result = (a != b); // true
cout << "2 != 4 is " << result << endl;
result = a > b; // false
cout << "2 > 4 is " << result << endl;
result = a < b; // true
cout << "2 < 4 is " << result << endl;
result = a >= b; // false
cout << "2 >= 4 is " << result << endl;
result = a <= b; // true
cout << "2 <= 4 is " << result << endl;
return 0;
}
Output
2 == 4 is 0 2 != 4 is 1 2 > 4 is 0 2 < 4 is 1 2 >= 4 is 0 2 <= 4 is 1
Note: Relational operators are used in decision making and loops.
C++ Logical Operators
- Logical operators are used to check whether an expression is true or false.
- If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Operator | Example | Meaning |
---|---|---|
&& |
expression1 && expression 2 | Logical AND. true only if all the operands are true. |
|| |
expression1 || expression 2 | Logical OR. true if at least one of the operands is true. |
! |
!expression | Logical NOT. true only if the operand is false. |
C++ Logical AND Operator
The logical AND operator &&
returns
true
- if and only if all the operands aretrue
.false
- if one or more operands arefalse
.
Truth Table of && Operator
Let a and b be two operands. 0 represents false while 1 represents true. Then,
a |
b |
a && b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Note: The Logical AND operator && should not be confused with the Bitwise AND operator &.
Example 2: C++ AND Operator
We can convert a char
array to double
by using the std::atof()
function.
For Example :-
// C++ program demonstrating && operator truth table
#include <iostream>
using namespace std;
int main() {
int a = 2;
int b = 4;
// false && false = false
cout << ((a == 0) && (a > b)) << endl;
// false && true = false
cout << ((a == 0) && (a < b)) << endl;
// true && false = false
cout << ((a == 2) && (a > b)) << endl;
// true && true = true
cout << ((a == 2) && (a < b)) << endl;
return 0;
}
Output
0 0 0 1
In this program, we declare and initialize two int
variables a and b with the values 2
and 4
respectively. We then print a logical expression
((a == 0) && (a > b))
Here, a == 0
evaluates to false
as the value of a is 2
. 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
The logical OR operator ||
returns
true
- if one or more of the operands aretrue
.false
- if and only if all the operands arefalse
.
Truth Table of || Operator
Let a and b be two operands. Then,
a |
b |
a || b |
---|---|---|
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.
Example 3: C++ OR Operator
This function accepts a number (can be any data type) and returns the number in the desired string.
// C++ program demonstrating || operator truth table
#include <iostream>
using namespace std;
int main() {
int a = 2;
int b = 4;
// false && false = false
cout << ((a == 0) || (a > b)) << endl;
// false && true = true
cout << ((a == 0) || (a < b)) << endl;
// true && false = true
cout << ((a == 2) || (a > b)) << endl;
// true && true = true
cout << ((a == 2) || (a < b)) << endl;
return 0;
}
Output
0 1 1 1
In this program, we declare and initialize two int
variables
a and b with the values 2
and
4
respectively. We then print a logical expression
((a == 0) || (a > b))
Here, a == 0
evaluates to false
as the value of
a is 2
. 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.
C++ Logical NOT 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,
Example 4: C++ NOT Operator
// C++ program demonstrating ! operator truth table
#include <iostream>
using namespace std;
int main() {
int a = 2;
// !false = true
cout << !(a == 0) << endl;
// !true = false
cout << !(a == 2) << endl;
return 0;
}
Output
1 0
In this program, we declare and initialize an int
variable
a with the value 2
. We then print a logical
expression
!(a == 0)
Here, a == 0
evaluates to false
as the value of
a is 2
. 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 == 2)
ultimately returns false
because a == 2
is true
.
We hope that this tutorial helped you develop better understanding of the concept of Relational and Logical Operators in C++.
Keep Learning : )