Use break to exit a loop immediately and continue to skip the current iteration and proceed to the next one.
Use break to exit a loop immediately and continue to skip the rest of the current iteration and jump to the next one.
for i := 0; i < 10; i++ {
if i == 3 {
continue // Skips printing 3
}
if i == 7 {
break // Exits the loop entirely
}
fmt.Println(i)
}
// Output: 0 1 2 4 5 6
Think of break as an emergency exit that stops the entire loop, while continue is like skipping a single step and moving straight to the next one. You use break when a condition is met and you are done looping, and continue when you want to ignore specific items but keep processing the rest.