C++ Code Example: if-else Statement on Boolean Expressions

This program demonstrates the use of boolean logic operators. It evaluates a logical expression using the variables a, b, and c, each of which is a boolean. The program then prints "True" or "False" based on the result of the expression, showcasing how logical operators like or and and work in conditional statements.

Code Example

#include <iostream>
using namespace std;

int main() {
    bool a = true, b = true, c = false;

    if ((a or b) and c) {
        cout << "True";
    } else {
        cout << "False";
    }

    return 0;
}

Output

False

Code Explanation

Variable Initialization

The program declares three boolean variables:

bool a = true, b = true, c = false;
  • a and b are set to true.
  • c is set to false.

Logical Expression

The if statement evaluates the following condition:

if ((a or b) and c)
  • (a or b) checks if either a or b is true. Since both are true, this evaluates to true.
  • and c then combines the result of (a or b) with the value of c. Since c is false, the entire condition evaluates to false.