×

go · level 1 · unit 4 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

is the other path at the fork.

An if on its own either runs its block or skips it. Often you want a plan B. else means otherwise: when the condition is false, Go runs the else block instead.

At a fork you always take exactly one path. With if and else, exactly one block runs. Never both, and never neither.

Write } else { all on one line: close the if block, say else, then open the new block. Put else on a line of its own and Go cannot read it.

package main
import "fmt"
func main() {
	biscuits := 0
	if biscuits == 0 {
		fmt.Println("Time to go to the shop.")
	} else {
		fmt.Println("Have a biscuit!")
	}
}

Read else as "otherwise".