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.
| left | right | left || right |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
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: incheck("left", true) || check("right", true), only the first call runs. - The else must follow the brace. Writing
elseon 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 anint, and Go reports a non-boolean condition. Compare explicitly instead. :=declares variables. It infers the types here:weatheris astringandtemperatureis anint. 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 istrue; with<it would befalse. - Repeating the variable wrongly.
weather != "sunny" || "cloudy"does not compile. Each side of||must be a full boolean expression. - Missing braces. A one-line
ifwithout braces is not valid Go.
Key takeaways
a || bis true when at least one ofaandbis true.- The right side is skipped when the left side is already true.
- An
elsebranch runs only when every part of the||was false. - Both sides must be booleans, and the braces and the
} else {layout are required.