Logical Operators
Logical operators in C
Logical operators in C are used to perform logical operations on the given expressions and to combine multiple conditions/constraints, Returns either 0 or 1, it depends on the expression result true or false.
The following are the logical operators in C:
&&
(logical AND):
- This operator returns 1 if both expressions are
true
or non zero , otherwise it returns 0. - The syntax for the
&&
operator is as follows:
if (expression1 && expression2)
{
// code to be executed if both expressions are true
}
Expression1 | Expression2 | Expression1 && Expression2 |
---|---|---|
true | true | (1) true |
true | false | (0) false |
false | true | (0) false |
false | false | (0) false |
||
(logical OR)
- This operator returns 1 if either of the expressions is
true
or non zero , otherwise it returns 0. - The syntax for the
||
operator is as follows:
if (expression1 || expression2)
{
// code to be executed if either of the expressions is true
}
Expression1 | Expression2 | Expression1 || Expression2 |
---|---|---|
true | true | (1) true |
true | false | (1) true |
false | true | (1) true |
false | false | (0) false |
!
(logical NOT)
- This operator returns 1 if the expression is
false
or zero, otherwise it returns 0. - The syntax for the
!
operator is as follows:
if (!expression)
{
// code to be executed if the expression is false
}
Expression | !Expression |
---|---|
true | (0) false |
false | (1) ture |
Example of Logical operators in C
#include <stdio.h>
int main()
{
int x = 5, y = 10;
// using && (logical AND) operator
if (x < 10 && y > 5)
{
printf("x is less than 10 and y is greater than 5\n");
}
else
{
printf("x is not less than 10 or y is not greater than 5\n");
}
// using || (logical OR) operator
if (x == 5 || y == 8)
{
printf("x is equal to 5 or y is equal to 8\n");
}
else
{
printf("x is not equal to 5 and y is not equal to 8\n");
}
// using ! (logical NOT) operator
if (!(x == y))
{
printf("x is not equal to y\n");
}
else
{
printf("x is equal to y\n");
}
return 0;
}
Output:
Output:
x is less than 10 and y is greater than 5
x is equal to 5 or y is equal to 8
x is not equal to y
In C, any non-zero value is considered true, and 0 is considered false.
Practical use cases of Logical operators
Here are some practical use cases of logical operators in C:
Conditional statements
Logical operators are often used in conditional statements such as if-else
, switch-case
, and loops
to check multiple conditions and execute different code based on the results.
Example:
if (x == 5 && y == 10)
{
printf("x is equal to 5 and y is equal to 10\n");
}
Input validation
Logical operators can be used to validate user input to ensure that the input is within a specified range or meets certain criteria.
Example:
if (age >= 18 || age <= 30)
{
printf("You are eligible to participate in the survey\n");
}
Error handling
Logical operators can be used to check for errors or exceptional conditions in a program and handle them appropriately.
Example:
if (!file_pointer)
{
printf("Error: Unable to open file\n");
}
Combining multiple conditions
Logical operators can be used to combine multiple conditions to check if all or any of the conditions are true and take appropriate action.
Example:
if (x > 10 || y < 20 || z == 30)
{
printf("At least one of the conditions is true\n");
}