Lesson 17 of 60 – Logical Operators in C
28%

Logical Operators in C

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.

Note: The three main logical operators in C are && (AND), || (OR), and ! (NOT).

1. What are Logical Operators?

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.

2. Logical Operators in C

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

3. Logical AND Operator &&

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.

4. AND When Both Conditions are True

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.

5. AND When One Condition is False

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.

6. AND Truth Table

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.

7. Logical OR Operator ||

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.

8. OR When Both Conditions are False

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.

9. OR When One Condition is True

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.

10. OR Truth Table

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.

11. Logical NOT Operator !

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.

12. NOT with a Nonzero Value

int value = 10;

printf("%d", !value);

Output:

0

Any nonzero value is considered true in a logical context, so ! changes it to false.

13. NOT Truth Table

Condition !Condition
False True
True False

The NOT operator always reverses the logical result.

14. Logical Operators with Relational Operators

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.

15. Checking Age Range

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.

16. Checking Student Eligibility

int age = 20;
int marks = 65;

if (age >= 18 && marks >= 40)
{
    printf("Eligible");
}
else
{
    printf("Not Eligible");
}

The student must satisfy both conditions.

17. Using OR for Alternatives

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.

18. Multiple OR Conditions

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.

19. Combining AND and OR

int age = 25;
int marks = 70;

if ((age >= 18 && marks >= 40) || age == 17)
{
    printf("Condition satisfied");
}

Parentheses make the intended grouping of conditions clear.

20. Using Parentheses with Logical Operators

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.

21. Logical Operators with User Input

#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;
}

22. Checking Pass Conditions

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.

23. Checking Login Conditions

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.

24. Checking Multiple Choices

char grade = 'A';

if (grade == 'A' || grade == 'B')
{
    printf("Good grade");
}

The OR operator allows more than one acceptable value.

25. Logical Operators in Loops

int i = 1;

while (i <= 10 && i != 5)
{
    printf("%d ", i);
    i++;
}

Logical operators can also be used in loop conditions.

26. Short-Circuit Evaluation

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.

27. Logical Operators and 0 / Nonzero Values

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

28. Common Logical Operator Mistakes

  • Confusing && with bitwise AND &.
  • Confusing || with bitwise OR |.
  • Using ! when the actual condition does not need to be reversed.
  • Forgetting that AND requires both conditions to be true.
  • Forgetting that OR requires only one condition to be true.
  • Writing complex expressions without clear parentheses.
  • Ignoring short-circuit evaluation when expressions have side effects.

29. Complete Logical Operators Program

#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.

30. Practice with Logical Operators

Practice the following programs:

  1. Check whether a number is between 1 and 100 using AND.
  2. Check whether a person is eligible based on age and marks.
  3. Check whether a day is Saturday or Sunday using OR.
  4. Check whether two conditions are both true.
  5. Use NOT to reverse a condition.
  6. Check whether a number is outside a given range.
  7. Create a simple login condition using AND.
  8. Accept multiple valid choices using OR.
  9. Use logical operators inside a loop.
  10. Create a program combining AND, OR, and NOT.

Practice logical operators with relational operators and decision-making statements to build strong programming logic.

📌 Key Points

  • && is the logical AND operator.
  • || is the logical OR operator.
  • ! is the logical NOT operator.
  • AND is true only when both operands are true.
  • OR is true when at least one operand is true.
  • NOT reverses a logical value.
  • In logical contexts, 0 represents false and nonzero values represent true.
  • Logical operators are commonly combined with relational operators.
  • Parentheses can make complex logical expressions clearer.
  • C uses short-circuit evaluation for logical AND and OR.

🧠 Quick Quiz

Question: Which logical operator is used when both conditions must be true?