break, continue and Labels
Loop control flow and labeled breaks
Loop Control Fundamentals
Go provides three keywords for controlling loop execution:
break— exits the current loop or switchcontinue— skips to the next iterationgoto— jumps to a labeled statement (rarely used)
Labels extend break and continue to work with outer loops.
break in a Loop
break immediately exits the innermost for, switch, or select statement:
for i := 0; i < 100; i++ {
if i*i > 50 {
break
}
fmt.Println(i)
}All lessons in this course
- if Statements and Init Expressions
- The for Loop: All Patterns
- switch Statements in Go
- break, continue and Labels