Logical Operator in c programming
Logical Operators in C Programming
Logical Operators: An expression that combines two or more expressions is termed a logical expression. For combining these expressions we use logical operators. These operators return 0 for false and 1 for true. The operands may be constants, variables, or expressions.
In C programming, logical operators are used to combine two or more conditions or expressions to form a single logical expression. There are three logical operators in C
logical operators are three types
- Logical AND (
&&
) - logical OR (
||
) - logical NOT (
!
)
Note:
- In this logical operator, first two operator is binary and the last two
- All these Operators will not modify the operand
- All these logical operators will gives us a result may be zero or one
Logical AND (&&) Operator
The logical AND operator gives the result true if both the conditions are true, otherwise, the result is false.
Boolean Table
A | B | A &&B |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Note:
- if The first operand is 0 we need to consider it as zero no need to check the rest operands
- if the first operand is 1 then we need to check the second opened.
Program 01:
#include
int main()
{
int i=10,j=20,k;
k=i&&j;
printf("k=%d\n",k);
}
Output: 1
Logical OR (II) Operator
The logical OR operator gives the result false, if both the conditions have the value false, otherwise the result is true anyone have true then the result is true
Boolean Table
A | B | A || B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Note:
- if the first operand is 1 then no need to check the rest operand just assign 1
- if the first operand is zero then we need to check the second operand
Program 02:
#include
int main()
{
int i=0,j=10,k;
k=i||(j=0);
printf("i=%d j=%d k=%d\n",i,j,k);
}
Attention: In this Program, we take Two operands,s, and Respectively we Assing the Values i=0 and j=10 After That we check the condition if both operands are false we got a false result if any of them are true we got true value.
Logical NOT (!) Operator:
Logical NOT is a unary operator, If the value of the condition is false then it gives the result true. If the value of the condition is true then it gives the result as false.
Boolean Table
A | !A |
0 | 1 |
1 | 0 |
Note:
- if it is Non zero we need to consider 0
- if it is zero we need to consider 1