w3resource

C Programming - Logical Operators Explained

Logical Operators with Examples in C

Overview

In C programming, logical operators are used to perform logical operations, typically to combine two or more conditions. These operators are widely used in control structures like if, while, and for statements.

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

Key Topics:

Logical AND (&&) with example

The logical AND operator checks if both conditions are true. If both conditions are true, the result is true; otherwise, it's false.

When to Use:

  • When to Use: Use && when you want an operation or block of code to execute only if all conditions are true.
  • Why to Use: It ensures that all the required conditions are met before proceeding, which is useful in decision-making processes where multiple checks are necessary.

Example:

Following example checks if two variables, a and b, are both positive using the logical AND (&&) operator.

Code:

#include <stdio.h>
int main() {
    int a = 5, b = 10;
    // Using logical AND operator
    if (a > 0 && b > 0) { // Check if both a and b are positive
        printf("Both a and b are positive.\n");
    } else {
        printf("One or both numbers are non-positive.\n");
    }
    return 0;
}

Output:

Both a and b are positive.

Explanation:

  • The variables a and b are initialized to 5 and 10, respectively.
  • The if statement checks if both a and b are greater than 0.
    • If both conditions (a > 0 and b > 0) are true, the program prints "Both a and b are positive."
    • If one or both conditions are false, the program prints "One or both numbers are non-positive."

In this case, since both a and b are positive, the output will be "Both a and b are positive."

Logical OR (||) with example

The logical OR operator checks if at least one condition is true. If any one of the conditions is true, the result is true.

  • When to Use: Use || when you want an operation or block of code to execute if any one of the conditions is true.
  • Why to Use: It allows for flexibility in decision-making, as the operation will proceed if any condition is satisfied, making it suitable for scenarios where one of several options can trigger an action.

Example:

This program checks if at least one of the two variables, a or b, is positive using the logical OR (||) operator.

Code:

#include <stdio.h>
int main() {
    int a = -5, b = 10;
    // Using logical OR operator
    if (a > 0 || b > 0) { // Check if at least one number is positive
        printf("At least one number is positive.\n");
    } else {
        printf("Neither number is positive.\n");
    }
    return 0;
}

Output:

At least one number is positive.

Explanation:

  • The variables a and b are initialized to -5 and 10, respectively.
  • The if statement checks if either a > 0 or b > 0 is true.
    • If either one or both conditions are true, the program prints "At least one number is positive."
    • If both conditions are false, it prints "Neither number is positive."

In this case, since b is positive, the output will be "At least one number is positive."

Logical NOT (!) with example

The logical NOT operator reverses the truth value of a condition. If the condition is true, ! makes it false, and if it's false, ! makes it true.

When to Use:

  • When to Use: Use ! when you need to check if a condition is not true.
  • Why to Use: It helps in negating a condition, which is useful when you want to ensure that a certain condition does not hold before proceeding with an action.

Example:

This program checks if the variable a is zero using the logical NOT (!) operator.

Code:

#include <stdio.h>
int main() {
    int a = 0;
    // Using logical NOT operator
    if (!a) { // Check if a is zero (as !0 is true)
        printf("a is zero.\n");
    } else {
        printf("a is non-zero.\n");
    }
    return 0;
}

Output:

a is zero.

Explanation:

  • The variable a is initialized to 0.
  • The if statement uses !a to check if a is zero. In C, !0 is true, so if a is zero, the condition will be true.
    • If a is zero, the program prints "a is zero."
    • If a is non-zero, it prints "a is non-zero."

Since a is 0, the output will be "a is zero."



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/c-logical-operators.php