Go · Stage 3 – Making Decisions · Lesson 22 of 64

else and else if

Choose an alternative path when an earlier condition is false.

Go code

ready := false
if ready {
    fmt.Println("Go")
} else {
    fmt.Println("Wait")
}
Line-by-line explanation
  1. Ready is false.
  2. The first block is skipped.
  3. Else supplies the alternative.
  4. Wait is displayed.
Expected output
Wait

This is the expected result.

else if checks another condition only when the earlier one was false.