Go - Break / Continue

Beginner 10/10 Teacher 10/10 Architect 10/10

Break and Continue

for i := 0; i < 10; i++ {
  if i == 5 { continue }
  if i == 8 { break }
}

Labels

outer:
for i := 0; i < 3; i++ {
  for j := 0; j < 3; j++ {
    if i*j > 2 { break outer }
  }
}

Common errors

  • Misusing labels; prefer them sparingly for nested loops only.

Practice

  • Search a 2D grid for the first zero and break out of both loops using a label.

Quick quiz

  1. What does continue do in a for loop?
Show answer Skips to the next iteration of the loop.