Course contentsShow
← Java
Lesson 139 of 2,8704. Control Flow StructuresFree lesson

Logical Operators in Conditions: && and ||

Combine multiple boolean expressions in a single if condition using AND and OR operators for more expressive tests.

Logical Operators in Conditions: && and ||

You'll learn how to combine multiple boolean tests in a single if statement to make more powerful decisions.

The Core Idea

Sometimes one condition isn't enough. You need to check two or more things at once. That's where logical operators shine. You've already seen && (AND) and || (OR) as standalone operators, but their real power emerges when you combine multiple boolean expressions directly in your if conditions.

Think of it like this: Imagine a bouncer at a club checking IDs. They might ask: "Are you 21 or older AND do you have valid ID?" Both conditions must be true. Or a streaming service: "Are you a premium member OR is this content free?" Either condition allows access.

Using && (AND) in Conditions

The && operator requires both sides to be true:

int age = 25;
boolean hasLicense = true;

if (age >= 18 && hasLicense) {
    // Only executes if BOTH conditions are true
    System.out.println("You can drive");
}

Using || (OR) in Conditions

The || operator succeeds if either side is true:

int score = 85;
boolean hasExtraCredit = true;

if (score >= 90 || hasExtraCredit) {
    // Executes if EITHER condition is true
    System.out.println("You pass with honors");
}

Combining Multiple Checks

You can chain several conditions together:

int temperature = 75;
boolean isSunny = true;
boolean isWeekend = false;

if (temperature > 70 && isSunny && isWeekend) {
    System.out.println("Perfect beach day!");
}

Key Takeaway: Use && when all conditions must be true, and || when any condition being true is sufficient. This lets you express complex real-world logic in a single, readable if statement.