break and continue

suggest change

Break

Break allows to exit for loop early.

i := 0
for {
	i++
	if i > 2 {
		break
	}
	fmt.Printf("i: %d\n", i)
}
i: 1
i: 2

Continue

Continue starts new iteration of for loop.

for i := 0; i < 3; i++ {
	if i == 1 {
		continue
	}
	fmt.Printf("i: %d\n", i)
}
i: 0
i: 2

Break/continue loop inside 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents