Home »
Golang
Go Continue Statement
Last Updated : July 19, 2025
In Go, the continue
statement is used to skip the current iteration of a loop and move on to the next one. It is commonly used to bypass certain conditions without breaking out of the loop entirely.
Using continue in a for Loop
The continue
statement can be used inside for
loops to skip specific iterations based on a condition.
Example
This example skips printing the number 5:
package main
import "fmt"
func main() {
for i := 1; i <= 10; i++ {
if i == 5 {
continue
}
fmt.Println(i)
}
}
When you run the above code, the output will be:
1
2
3
4
6
7
8
9
10
Using continue in a for-range Loop
The continue
statement can also be used with for-range
loops to skip specific values.
Example
This example skips printing odd numbers in a slice:
package main
import "fmt"
func main() {
nums := []int{1, 2, 3, 4, 5, 6}
for _, num := range nums {
if num%2 != 0 {
continue
}
fmt.Println(num)
}
}
When you run the above code, the output will be:
2
4
6
Using continue in Nested Loops
When used in nested loops, continue
affects only the innermost loop where it is called.
Example
This example demonstrates the effect of continue
in nested loops:
package main
import "fmt"
func main() {
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if j == 2 {
continue
}
fmt.Printf("i=%d, j=%d\n", i, j)
}
}
}
When you run the above code, the output will be:
i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3
Using Labeled continue
In Go, you can use a label with continue
to skip to the next iteration of an outer loop.
Example
This example uses a labeled continue
to continue the outer loop:
package main
import "fmt"
func main() {
outer:
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if i == j {
continue outer
}
fmt.Printf("i=%d, j=%d\n", i, j)
}
}
}
When you run the above code, the output will be:
i=1, j=2
i=2, j=1
i=3, j=1
i=3, j=2
Exercise
Select the correct answer for each question about the continue
statement in Go.
- What does the
continue
statement do in Go?
- Which loop will a plain
continue
affect when used in nested loops?
- Which keyword combination is required to skip to the next iteration of an outer loop?
Advertisement
Advertisement