C++ or Keyword

The or keyword in C++ is an alternative representation of the logical OR operator (||). It is part of the alternative tokens introduced in the C++ Standard to improve code readability and provide an alternative for non-English keyboard layouts.

Like ||, the or keyword evaluates to true if at least one of its operands evaluates to true. If both operands evaluate to false, the result is false.


Syntax

</>
Copy
expression1 or expression2
or
The keyword representing the logical OR operation.
expression1, expression2
The operands being compared. These should evaluate to Boolean values.

Examples

Example 1: Basic Use of or Keyword

This example demonstrates the equivalence of or and the logical OR operator (||).

</>
Copy
#include <iostream>
using namespace std;

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

    cout << "Using || operator: " << (a || b) << endl;
    cout << "Using or keyword: " << (a or b) << endl;

    return 0;
}

Output:

Using || operator: 1
Using or keyword: 1

Explanation:

  1. The variables a and b are true and false, respectively.
  2. Both a || b and a or b evaluate to true, as at least one operand is true.
  3. The output confirms that or is functionally identical to ||.

Example 2: Using or in Conditional Statements

The or keyword can be used in conditional statements for logical comparisons.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = 10;

    if (x == 5 or y == 20) {
        cout << "At least one condition is true." << endl;
    } else {
        cout << "Both conditions are false." << endl;
    }

    return 0;
}

Output:

At least one condition is true.

Explanation:

  1. The first condition x == 5 evaluates to true.
  2. The second condition y == 20 evaluates to false.
  3. Since one condition is true, the or operator results in true, and the if block executes.

Example 3: Combining or with Logical AND

The or keyword can be combined with logical and or not for complex conditions.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    bool isOnline = true;
    bool isInMaintenance = false;
    bool isAdmin = false;

    if ((isOnline or isAdmin) and not isInMaintenance) {
        cout << "Service is accessible." << endl;
    } else {
        cout << "Service is not accessible." << endl;
    }

    return 0;
}

Output:

Service is accessible.

Explanation:

  1. The condition (isOnline or isAdmin) evaluates to true because isOnline is true.
  2. The condition not isInMaintenance evaluates to true because isInMaintenance is false.
  3. Both conditions together result in true, and the if block executes.

Key Points to Remember about or Keyword

  • or is an alternative representation of the logical OR operator (||).
  • It evaluates to true if at least one of its operands evaluates to true.
  • It is functionally identical to || and can be used interchangeably.
  • It is part of the alternative tokens introduced to improve readability and support different keyboard layouts.
  • Using or is optional and depends on coding style or preference.