Start learning free

LESSON 78 OF 338 · CONDITIONAL STATEMENTS

Or Operator in Go: Combining Conditions with ||

Lesson Read the full explanation

The Go || operator means or.

A condition using || is true when at least one side is true:

weather := "sunny"
temperature := 90

if weather != "sunny" || temperature <= 85 {
    fmt.Println("Learning to code")
} else {
    fmt.Println("Going to the beach")
}

Here neither "bad beach condition" is true, so the else branch runs.

Assignment
Use || to check whether either condition keeps us from the beach.


STEP 1 OF 2
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Or Operator in Go, explained

    What the or operator does

    The || operator joins two boolean expressions into one. The result is true when at least one side is true, and false only when both sides are false. Use it when several different situations should lead to the same action.

    leftrightleft || right
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    Both sides must be booleans. In Go that means comparisons such as temperature <= 85, boolean variables, or function calls that return bool. Numbers and strings are never treated as true or false on their own.

    The lesson's exercise

    The code declares two variables:

    weather := "sunny"
    temperature := 90
    

    The goal is to go to the beach only when the weather is sunny and the temperature is above 85. Put the other way round, two things can keep you at your desk: the weather is not sunny, or the temperature is 85 or lower. Those two conditions are exactly what || joins.

    Step 1: the if statement

    Write each condition as a comparison. weather != "sunny" is true when the weather is anything but sunny, and temperature <= 85 is true when it is not hot enough. Join them with || and print the message when the combined condition is true. Go does not need parentheses around an if condition, but the braces around the body are required, even for one statement.

    Step 2: the else branch

    The else branch has no condition of its own. It runs whenever the if condition is false, which for an || means both sides were false: the weather is "sunny" and the temperature is above 85. Write it as } else { on the line where the if body closes.

    The solution in Go

    weather := "sunny"
    temperature := 90
    
    if weather != "sunny" || temperature <= 85 {
        fmt.Println("Learning to code")
    } else {
        fmt.Println("Going to the beach")
    }
    

    With these values weather != "sunny" is false and temperature <= 85 is false too, so the whole condition is false and the program prints Going to the beach. Change temperature to 80 and the second comparison becomes true, so the program prints Learning to code.

    Go details worth knowing

    • Short-circuit evaluation. Go evaluates the left side first. If it is already true, the right side is never evaluated, because the result cannot change. This matters when the right side is a function call with a visible effect: in check("left", true) || check("right", true), only the first call runs.
    • The else must follow the brace. Writing else on its own line after the closing } is a syntax error (unexpected else, expected }), so the program doesn't build.
    • The condition must be a bool. if temperature { does not compile for an int, and Go reports a non-boolean condition. Compare explicitly instead.
    • := declares variables. It infers the types here: weather is a string and temperature is an int. Go rejects a declared variable that is never used, so both are read by the condition.
    • Single | is different. It is the bitwise or on integers, not a logical operator, and it does not work on booleans.

    Common mistakes

    • Using && by accident. The statement "not sunny and not hot" would need && and would give a different program. Read the sentence you are encoding: "either one is enough" means ||.
    • Wrong direction of a comparison. The lesson sends the learner to code when the temperature is 85 or lower, so <= is correct. At exactly 85 the condition is true; with < it would be false.
    • Repeating the variable wrongly. weather != "sunny" || "cloudy" does not compile. Each side of || must be a full boolean expression.
    • Missing braces. A one-line if without braces is not valid Go.

    Key takeaways

    • a || b is true when at least one of a and b is true.
    • The right side is skipped when the left side is already true.
    • An else branch runs only when every part of the || was false.
    • Both sides must be booleans, and the braces and the } else { layout are required.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free