break and continue
suggest changeBreak
break exits a for loop early.
i := 0
for {
i++
if i > 2 {
break
}
fmt.Printf("i: %d\n", i)
}
i: 1
i: 2
Continue
continue starts the next iteration of a for loop.
for i := 0; i < 3; i++ {
if i == 1 {
continue
}
fmt.Printf("i: %d\n", i)
}
i: 0
i: 2
An unlabeled break inside a switch leaves only the switch, not the surrounding loop. Label the loop to break or continue that loop from inside the switch:
package main
import "fmt"
func main() {
j := 100
loop:
for j < 110 {
j++
switch j % 3 {
case 0:
continue loop
case 1:
break loop
}
fmt.Println("Var : ", j)
}
}
Var : 101
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents