Combining conditions with &&, || and !
Sometimes one comparison is not enough. A shop gives free shipping when the order is at least 50 and the customer is a member. A ride is open to people who are tall enough or who come with an adult. The logical operators join comparisons like this:
| Operator | Name | true when |
|---|---|---|
&& | and | both sides are true |
|| | or | at least one side is true |
! | not | the value after it is false |
Here is && in a full program:
package main
import "fmt"
func main() {
total := 64
member := true
if total >= 50 && member {
fmt.Println("Free shipping")
} else {
fmt.Println("Shipping costs 5")
}
}
Output:
Free shipping
Both sides are true, so the whole condition is true. Set member to false, and the result changes to Shipping costs 5, even though the total is still high enough.
member is already a bool, so you can use it directly. You do not need to write member == true.
|| needs only one true side, and ! turns true into false and false into true:
height := 112
withAdult := true
if height >= 120 || withAdult {
fmt.Println("You can ride")
}
raining := false
if !raining {
fmt.Println("No umbrella needed")
}
Output:
You can ride
No umbrella needed
Go stops as soon as it knows
Go reads a condition from left to right, and it stops early when the answer is already known. With &&, if the left side is false, the whole thing must be false, so Go never looks at the right side. With ||, a true left side is enough. This is called short-circuit evaluation.
It lets the left side protect the right side. Dividing a whole number by zero crashes a Go program, so this check tests people first:
total := 12
people := 0
if people > 0 && total/people >= 5 {
fmt.Println("Enough for everyone")
} else {
fmt.Println("Not enough")
}
Output:
Not enough
people > 0 is false, so total/people never runs. Swap the two sides, and the division runs first:
total := 12
people := 0
if total/people >= 5 && people > 0 {
fmt.Println("Enough for everyone")
}
Output:
panic: runtime error: integer divide by zero
Mixing && and ||
When a condition has both, Go does && before ||, the same way it does * before +. That can give a surprising answer. The rule here is "a member or a ticket holder, and at least 18":
member := true
ticket := false
age := 15
fmt.Println(member || ticket && age >= 18)
fmt.Println((member || ticket) && age >= 18)
Output:
true
false
The first line is read as member || (ticket && age >= 18), so a 15-year-old member gets in. The parentheses in the second line say what the rule means. When you mix && and ||, always add parentheses.
A common mistake
In math you can write 1 < x < 10. In Go you cannot. Each comparison needs both of its sides:
package main
import "fmt"
func main() {
x := 5
if 1 < x < 10 {
fmt.Println("in range")
}
}
Output:
# command-line-arguments
./main.go:7:13: invalid operation: 1 < x < 10 (mismatched types untyped bool and untyped int)
Go works out 1 < x first, which gives a bool, and a bool cannot be compared with 10. Write x > 1 && x < 10 instead.
In the exercise, read a visitor's age and two yes-or-no answers, and decide with &&, || and parentheses whether the visitor may enter.