Logical operators are used to combine or reverse conditions in C. They are especially useful when a program needs to check multiple conditions at the same time.
&& (AND), || (OR), and ! (NOT).
Logical operators are used to combine or modify conditions. They are commonly used with relational expressions inside decision-making statements.
int age = 20;
if (age >= 18 && age <= 60)
{
printf("Valid age");
}
Here, two conditions are combined using the logical AND operator.
| Operator | Name | Purpose |
|---|---|---|
&& |
Logical AND | True when both conditions are true |
|| |
Logical OR | True when at least one condition is true |
! |
Logical NOT | Reverses a logical value |
The logical AND operator && produces true only when both
operands are nonzero.
int a = 10;
int b = 20;
printf("%d", a < 20 && b > 10);
Output:
1
Both conditions are true, so the result is 1.
int age = 25;
int marks = 70;
if (age >= 18 && marks >= 40)
{
printf("Both conditions are true");
}
Output:
Both conditions are true
The message is displayed because both conditions are satisfied.
int age = 16;
int marks = 70;
if (age >= 18 && marks >= 40)
{
printf("Eligible");
}
else
{
printf("Not eligible");
}
Output:
Not eligible
The first condition is false, so the complete AND expression is false.
| Condition A | Condition B | A && B |
|---|---|---|
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
For logical AND, both conditions must be true for the result to be true.
The logical OR operator || produces true when at least one
operand is nonzero.
int age = 16;
printf("%d", age < 18 || age == 25);
Output:
1
The first condition is true, so the complete OR expression is true.
int age = 30;
if (age < 18 || age == 25)
{
printf("Condition true");
}
else
{
printf("Condition false");
}
Output:
Condition false
Both conditions are false, so the OR expression is false.
int marks = 80;
if (marks >= 90 || marks >= 60)
{
printf("Condition true");
}
Output:
Condition true
Only one condition needs to be true for the OR expression to be true.
| Condition A | Condition B | A || B |
|---|---|---|
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
For logical OR, at least one condition must be true.
The logical NOT operator ! reverses the logical value of its
operand.
int value = 0;
printf("%d", !value);
Output:
1
Since 0 represents false, applying ! makes it true.
int value = 10;
printf("%d", !value);
Output:
0
Any nonzero value is considered true in a logical context, so
! changes it to false.
| Condition | !Condition |
|---|---|
| False | True |
| True | False |
The NOT operator always reverses the logical result.
Logical operators are frequently combined with relational operators.
int age = 25;
if (age >= 18 && age <= 60)
{
printf("Age is in range");
}
Here, >= and <= create the relational
conditions, while && combines them.
int age = 25;
if (age >= 18 && age <= 60)
{
printf("Valid age");
}
else
{
printf("Invalid age");
}
The AND operator ensures that both boundary conditions are satisfied.
int age = 20;
int marks = 65;
if (age >= 18 && marks >= 40)
{
printf("Eligible");
}
else
{
printf("Not Eligible");
}
The student must satisfy both conditions.
The OR operator is useful when there are multiple acceptable alternatives.
int day = 6;
if (day == 6 || day == 7)
{
printf("Weekend");
}
The condition is true when the day is either 6 or 7.
int day = 3;
if (day == 1 || day == 3 || day == 5)
{
printf("Selected day");
}
The OR operator allows the program to accept any one of several conditions.
int age = 25;
int marks = 70;
if ((age >= 18 && marks >= 40) || age == 17)
{
printf("Condition satisfied");
}
Parentheses make the intended grouping of conditions clear.
Parentheses should be used when they make a complex logical expression easier to understand or when a specific grouping is required.
if ((age >= 18 && marks >= 40) || special == 1)
{
printf("Eligible");
}
This expression first groups the AND condition and then combines it with the OR condition.
#include <stdio.h>
int main()
{
int age;
printf("Enter age: ");
scanf("%d", &age);
if (age >= 18 && age <= 60)
{
printf("Age is valid");
}
else
{
printf("Age is outside the range");
}
return 0;
}
int theory = 50;
int practical = 60;
if (theory >= 40 && practical >= 40)
{
printf("Pass");
}
else
{
printf("Fail");
}
Both theory and practical marks must meet the minimum requirement.
int usernameCorrect = 1;
int passwordCorrect = 1;
if (usernameCorrect && passwordCorrect)
{
printf("Login successful");
}
else
{
printf("Login failed");
}
The AND operator is useful when all required conditions must be satisfied.
char grade = 'A';
if (grade == 'A' || grade == 'B')
{
printf("Good grade");
}
The OR operator allows more than one acceptable value.
int i = 1;
while (i <= 10 && i != 5)
{
printf("%d ", i);
i++;
}
Logical operators can also be used in loop conditions.
C uses short-circuit evaluation for && and
||.
With &&, if the left operand is false, the right operand
does not need to be evaluated.
With ||, if the left operand is true, the right operand does not
need to be evaluated.
if (x != 0 && 10 / x > 2)
{
printf("Condition true");
}
The first condition prevents the division expression from being evaluated
when x is zero.
In C, 0 represents false in a logical context, while any
nonzero value represents true.
int a = 5;
int b = 0;
printf("%d\n", a && b);
printf("%d\n", a || b);
printf("%d\n", !b);
Output:
0
1
1
&& with bitwise AND &.|| with bitwise OR |.! when the actual condition does not need to be reversed.#include <stdio.h>
int main()
{
int age;
int marks;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter marks: ");
scanf("%d", &marks);
if (age >= 18 && marks >= 40)
{
printf("Eligible\n");
}
else if (age < 18 || marks < 40)
{
printf("Not eligible\n");
}
return 0;
}
This example uses AND and OR operators with relational expressions.
Practice the following programs:
Practice logical operators with relational operators and decision-making statements to build strong programming logic.
&& is the logical AND operator.|| is the logical OR operator.! is the logical NOT operator.0 represents false and nonzero values represent true.Question: Which logical operator is used when both conditions must be true?