C Logical Operators - C++ operators - AND, OR, NOT
Logical Operators
Logical Operators are used to check the logical relationship between two expressions. After evaluating the conditions, it provides logical true (non-zero value) or logical false (zero value) status. Here, the operands can be constants, variable and expressions.
| Operator | Name | Description | Example |
|---|---|---|---|
| && | Logical AND | if both conditions are true, then it returns true | 5 > 3 && 5 < 10 is true |
| | | | Logical OR | if any one of two conditions is true, then it returns true | 5 < 3 | | 5 < 10 is true |
| ! | Logical NOT | if the conditions is false, then it returns true | !(5 > 3) is false |
Example
Following is an example C Program using Logical Operators.
#include <stdio.h>
#include <conio.h>
int main()
{
int a=5;
int b=3;
if((a>3)&&(a<10)){
printf("The Logical AND Expression is True\n");
} else{
printf("The Logical AND Expression is False\n ");
}
if((b>a) || (b<10)){
printf("The Logical OR Expression is True \n ");
} else{
printf("The Logical OR Expression is False \n ");
}
if(!(a>b)){
printf("The Logical NOT Expression is True \n ");
} else{
printf("The Logical NOT Expression is False \n");
}
return(0);
}
Output
The Logical AND Expression is True The Logical OR Expression is True The Logical NOT Expression is False