Logical Operators –
Logical Operators performs logical operations on the given two variables. It tells how the relationships among values/operands can be connected.
C++ provides three Logical Operators –
The Truth table for Logical Operators will be –
| x |
y |
x||y |
x && y |
!(x) |
| 0 |
0 |
0 |
0 |
1 |
| 0 |
1 |
1 |
0 |
1 |
| 1 |
0 |
1 |
0 |
0 |
| 1 |
1 |
1 |
1 |
0 |
Table shows the working of Logical Operators –
| S.no |
Operators |
Name |
Example |
Result |
Description |
| 1 |
&& |
logical AND |
(6>5)&&(3<5) |
1 |
Returns Boolean truth value
(0 for false and 1 for true).
Both expressions must return 1
for && to return. |
| 2 |
|| |
logical OR |
(6>5)||(7<5) |
1 |
returns true when at-least one
of the condition is true |
| 3 |
! |
logical NOT |
!(6<=6)
!(5>9) |
0
1 |
Negates the result of
expression |
#include <iostream.h>
int main()
{
int k=40,l=20;
int m=20,n=30;
if (k>l && k !=0)
cout<<"&& Operator : Both conditions are true\n";
if (m>n || n!=20)
cout<<"|| Operator : Only one condition is true\n";
if (!(m>n && m !=0))
cout<<"! Operator : Both conditions are true\n";
else
{
cout<<"! Operator : Both conditions are true.\n"
<<“But, status is inverted as false”);
}
return 0;
}
Output :
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
]]>