Go · Stage 4 – Repeating Things · Lesson 27 of 64

Infinite Loops and break

Create an intentional endless loop with a clear exit.

Go code

count := 0
for {
    count++
    if count == 2 {
        break
    }
}
fmt.Println(count)
Line-by-line explanation
  1. Count starts at zero.
  2. For begins an endless loop.
  3. Count increases.
  4. The if finds 2 and break exits.
  5. The final count is displayed.
Expected output
2

This is the expected result.