There are three logical operators in C language. Assume variable A and B holds 1 and 0 respectively then –
| Operator | Description | Example |
|---|---|---|
| && | AND operator (the condition becomes true if both the operands are non-zero) | (A && B) is false. |
| || | OR Operator (the condition becomes true if any of the two operands is non-zero) | (A || B) is true. |
| ! | NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) | !(A && B) is true. |
Example: Check both numbers are greater the 10 or not
#include < stdio.h >
int main() {
int a, b, big;
a = 20;
b = 20;
if (a > 10 && b > 10) // if condition is true move inside if-statement
{
printf("a and b both are greater then 10");
}
return 0;
}
Output
a and b both are greater then 10
Example: Check both numbers are greater the 10 or not. Also check any one number is greater then 10 or not
#includeint main() { int a, b, big; a = 20; b = 5; if (a >10 && b>10) // if condition is false { printf("a and b both are greater then 10"); } if (a >10 || b>10) // if condition is true move inside if-statement { printf("Either a or b is greater then 10"); } return 0; }
Output
Either a or b is greater then 10