- These operators are used to perform logical operations on the given expressions.
- There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
S.no | Operators | Name | Example | Description |
1 | && | logical AND | (x>5)&&(y<5) | It returns true when both conditions are true |
2 | || | logical OR | (x>=10)||(y>=10) | It returns true when at-least one of the condition is true |
3 | ! | logical NOT | !((x>5)&&(y<5)) | It reverses the state of the operand “((x>5) && (y<5))” If “((x>5) && (y<5))” is true, logical NOT operator makes it false |
Example program for logical operators in C:
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf(“&& Operator : Both conditions are true\n”);
}
if (o>p || p!=20)
{
printf(“|| Operator : Only one condition is true\n”);
}
if (!(m>n && m !=0))
{
printf(“! Operator : Both conditions are true\n”);
}
else
{
printf(“! Operator : Both conditions are true. ” \
“But, status is inverted as false\n”);
}
}
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf(“&& Operator : Both conditions are true\n”);
}
if (o>p || p!=20)
{
printf(“|| Operator : Only one condition is true\n”);
}
if (!(m>n && m !=0))
{
printf(“! Operator : Both conditions are true\n”);
}
else
{
printf(“! Operator : Both conditions are true. ” \
“But, status is inverted as false\n”);
}
}
Output:
&& Operator : Both conditions are true || Operator : Only one condition is true ! Operator : Both conditions are true. But, status is inverted as false |
- In this program, operators (&&, || and !) are used to perform logical operations on the given expressions.
- && operator – “if clause” becomes true only when both conditions (m>n and m! =0) is true. Else, it becomes false.
- || Operator – “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It becomes false when none of the condition is true.
- ! Operator – It is used to reverses the state of the operand.
- If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!” operator.
- So, “! (m>n and m! =0)” returns false (0).
No comments:
Post a Comment