In this C++ tutorial, you will learn how the logical OR operator || combines conditions. You will see its truth table, use it with boolean values and if statements, understand short-circuit evaluation, and compare || with &&, |, and |=.

C++ OR Logical Operator (||)

C++ OR Logical Operator is used to combine two or more logical conditions to form a compound condition. || is the symbol used for C++ OR Operator.

C++ OR Operator takes two boolean values as operands and returns a boolean value.

</>
Copy
operand_1 || operand_2

The result is true when at least one operand is true. The result is false only when both operands are false.

C++ || Operator Truth Table

Following is the truth table of C++ OR Logical Operator.

Operand 1Operand 2Returns
truetrue1
truefalse1
falsetrue1
falsefalse0

C++ OR returns true even if one of the operand is true.

C++ || Operator with Boolean Values

The following example demonstrates the usage of OR logical operator (||) with different boolean values.

main.cpp

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

int main() {
   cout << (true || true) << endl;
   cout << (true || false) << endl;
   cout << (false || true) << endl;
   cout << (false || false) << endl;
}

Output

1
1
1
0

By default, cout prints true as 1 and false as 0. Therefore, the first three expressions print 1, while false || false prints 0.

C++ OR Operator in an if Statement

The || operator is commonly used in an if statement when any one of several conditions is enough for the statement to succeed.

The following example demonstrates the usage of OR logical operator (||) in combining boolean conditions and forming a compound condition.

main.cpp

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

int main() {
   int a = 7;

   if ((a < 10) || (a%2 == 0)) {
      cout << "a is even or less than 10." << endl;
   }
}

Output

a is even or less than 10.

In the above example, a<10 checks whether a is less than 10, and a%2==0 checks whether a is an even number.

For a = 7, the first condition is true and the second condition is false. Because only one condition needs to be true for logical OR, the complete expression evaluates to true and the message is printed.

Combining Three or More Conditions with || in C++

You can chain multiple logical OR operators when any one of several alternatives should make the complete condition true.

</>
Copy
condition1 || condition2 || condition3

For example, the following program checks whether a character is one of three accepted choices.

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

int main() {
    char choice = 'y';

    if (choice == 'y' || choice == 'Y' || choice == '1') {
        cout << "Accepted";
    }

    return 0;
}
Accepted

The condition is true because choice == 'y' is true. C++ does not require the remaining alternatives to be true.

Short-Circuit Evaluation of || in C++

The logical OR operator uses short-circuit evaluation. C++ evaluates the left operand first. If it is already true, the complete OR expression must be true, so the right operand is not evaluated.

This matters when the second condition performs work or should only be evaluated when the first condition fails.

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

bool checkSecondCondition() {
    cout << "Second condition checked" << endl;
    return true;
}

int main() {
    bool first = true;

    if (first || checkSecondCondition()) {
        cout << "Condition is true";
    }

    return 0;
}
Condition is true

The function checkSecondCondition() is not called because first is already true. If first were false, C++ would evaluate the function call to determine the result.

Difference Between || and && in C++

|| and && are both logical operators, but they answer different questions. Use OR when any condition may be true. Use AND when every required condition must be true.

OperatorMeaningWhen the result is true
||Logical ORAt least one operand is true
&&Logical ANDBoth operands are true

For example, day == 6 || day == 7 can test whether a day number represents either of two alternatives, while age >= 18 && age <= 60 requires both limits to be satisfied.

Difference Between || and | in C++

The double-pipe operator || is logical OR. The single-pipe operator | is primarily the bitwise OR operator for integral values. They should not be treated as interchangeable.

OperatorTypical useShort-circuits?
||Combining logical conditionsYes
|Bitwise OR of integer valuesNo

When boolean expressions are used with |, both operands are evaluated. With ||, the right operand is skipped when the left operand is already true. For normal condition checks in an if statement, || is usually the intended operator.

What |= Means Compared with || in C++

|= is a compound assignment operator, not the logical OR operator. The expression a |= b performs a bitwise OR between a and b, then stores the result back in a.

</>
Copy
a |= b;

It is therefore different from a || b, which evaluates a logical expression and produces a boolean result without assigning that result back to a.

The Alternative or Operator Token in C++

C++ also provides the keyword or as an alternative token for ||. The two forms have the same logical meaning and precedence.

</>
Copy
condition1 or condition2

For example, x == 0 or y == 0 is equivalent to x == 0 || y == 0. Both forms are valid C++.

Operator Precedence When Using || in C++

Comparison operators such as ==, <, and > are evaluated before logical OR. Logical AND && also has higher precedence than logical OR ||.

For example, the following expression evaluates the AND operation before the OR operation:

</>
Copy
a || b && c

It is interpreted as a || (b && c). When a condition mixes || and &&, parentheses can make the intended grouping clearer.

Common Mistakes with the C++ || Operator

  • Using | instead of ||: | is primarily a bitwise operator and does not short-circuit.
  • Expecting both conditions to be true: logical OR needs only one true operand.
  • Writing a repeated variable test incorrectly: write x == 1 || x == 2, not x == 1 || 2.
  • Assuming the right operand always runs: it may be skipped when the left operand is true.
  • Confusing || with |=: || is logical OR, while |= is a bitwise compound assignment operator.

Key Rules for the C++ Logical OR Operator

  • Use || when at least one of several conditions may make an expression true.
  • The result is false only when every OR operand is false.
  • || evaluates operands from left to right and short-circuits after a true operand determines the result.
  • || is different from the bitwise OR operator | and the compound assignment operator |=.
  • The keyword or is a valid alternative token for || in C++.

C++ OR Logical Operator Summary

In this C++ Tutorial, we learned what C++ OR Logical Operator is, and how to use it with conditional expressions.