Logical OR Operator: ||
You'll learn how the || operator lets you combine boolean conditions so that at least one must be true for the overall result to be true.
What is the Logical OR Operator?
The || operator (read as "logical OR") takes two boolean values and returns true if at least one of them is true. Only when both operands are false does it return false.
Think of it like a safety system with two backup switches: the alarm sounds if either switch is triggered—you don't need both. One is enough.
How || Works
Here's the truth table:
true || true→truetrue || false→truefalse || true→truefalse || false→false
Notice that as long as one side is true, the result is true.
Examples with Code
boolean hasTicket = true;
boolean isVIP = false;
boolean canEnter = hasTicket || isVIP; // true (ticket is enough)
int age = 15;
boolean isChild = age < 12;
boolean isTeen = age >= 13;
boolean getsDiscount = isChild || isTeen; // true (teen qualifies)
boolean hasCoupon = false;
boolean isMember = false;
boolean getsDeal = hasCoupon || isMember; // false (neither is true)
You can also combine || with relational operators directly:
int score = 85;
boolean passed = score >= 60 || score < 0; // true (first condition met)
Key Takeaway: The || operator returns true when at least one of its boolean operands is true, making it perfect for situations where multiple conditions can satisfy a requirement.