×

go · level 1 · unit 5 · lesson 3

  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

Put an if inside a loop to decide every round.

An if can live inside a loop, so the program decides something on every round. Picture walking down a street and knocking only on the red doors.

% gives the remainder. A number is when n%2 == 0, because it splits between two with nothing left over. Otherwise it is .

To count matches, make a box holding 0 before the loop. Inside, each time the if is true, add 1. After the loop, the box holds the total.

package main
import "fmt"
func main() {
	evens := 0
	for n := 1; n <= 6; n++ {
		if n%2 == 0 {
			fmt.Println(n, "is even")
			evens = evens + 1
		}
	}
	fmt.Println("Even numbers:", evens)
}

Make the counting box before the loop starts, and show it after the loop ends.