In C++, the OR operator is written ||. It combines two conditions and gives true when at least one of them is true. It gives false only when both are false. C++ also accepts the word or as a spelling of the same operator. The single bar | is a different operator, the bitwise OR, which works on the bits of integers.
Logical OR with ||
The basic form is:
condition1 || condition2
There are four possible results:
true || trueistruetrue || falseistruefalse || trueistruefalse || falseisfalse
This program checks whether someone can enter. They need to be 18 or older, or have a ticket:
#include <iostream>
int main() {
int age = 17;
bool hasTicket = true;
if (age >= 18 || hasTicket) {
std::cout << "Entry allowed\n";
} else {
std::cout << "Entry denied\n";
}
return 0;
}
age >= 18 is false, but hasTicket is true. Because one side is true, the whole condition is true, and the program prints Entry allowed.
What the result looks like
|| always gives a bool. Its operands don’t have to be bool, though. C++ treats 0 as false and any other number as true, so 0 || 5 is true.
By default, std::cout prints a bool as 1 or 0. Use std::boolalpha to print true or false:
#include <iostream>
int main() {
std::cout << (0 || 5) << "\n"; // 1
std::cout << std::boolalpha << (0 || 5) << "\n"; // true
return 0;
}
Short-circuit evaluation
|| checks its left side first. If the left side is true, the result is already known, so C++ skips the right side. This is called short-circuit evaluation.
#include <iostream>
bool checkValue() {
std::cout << "checkValue() was called\n";
return true;
}
int main() {
bool result = true || checkValue();
std::cout << std::boolalpha << result << "\n";
return 0;
}
Output:
true
checkValue() never runs, so its message never prints. When the left side is false, C++ evaluates the right side to get the answer.
Short-circuiting lets you put a safety check first. This code is safe even when ptr is null:
if (ptr == nullptr || ptr->value == 0) {
// ptr is null, or ptr->value is 0
}
If ptr is null, the first condition is true, so ptr->value is never read. Reversing the two sides would read through a null pointer, which is undefined behavior.
Precedence with && and comparisons
&& binds tighter than ||. So this line:
a || b && c
means a || (b && c). When you mix the two, add parentheses so the reader doesn’t have to remember the rule.
Comparison operators such as == and >= bind tighter than both, so this works without extra parentheses:
if (score >= 90 || attendance == 100) {
A common bug: a bare number on one side
Each side of || should be a complete condition. This is a common mistake:
int n = 5;
if (n == 1 || 2) { // always true
std::cout << "Matched\n";
}
n == 1 is false, but 2 on its own counts as true, because any nonzero value does. So the condition is always true. Write out the comparison in full:
if (n == 1 || n == 2) {
Bitwise OR with |
The single | works on each bit of two integers. A bit in the result is 1 when at least one of the matching input bits is 1.
#include <iostream>
int main() {
int a = 5; // binary 0101
int b = 3; // binary 0011
std::cout << "a | b = " << (a | b) << "\n"; // 7, binary 0111
std::cout << "a || b = " << (a || b) << "\n"; // 1, meaning true
return 0;
}
Output:
a | b = 7
a || b = 1
Two differences matter most:
||works on conditions and givestrueorfalse.|works on the bits of integers and gives an integer.||short-circuits.|always evaluates both sides, so a function call on its right side always runs.
Other bit operators, such as the shift operator in c, move bits instead of combining them.
Using or instead of ||
The word or is an alternative token for ||, and the two behave the same:
if (age >= 18 or hasTicket) {
The symbol is far more common in code and examples, so that is the form you will see most.
Quick reference
a || bistruewhen either side istrue. It skipsbifaistrue.a or bmeans the same asa || b.a | bcombines the bits of two integers. Both sides are always evaluated.&&binds tighter than||, so use parentheses when you mix them.- The
!operator flips abool. It takes one operand, so it is one of the unary operators in c.
Where to go next
If you are new to operators, start with what are operators in c with examples, which lists every group. If you need to choose between two values, the conditional operator in c does that with ?:.