Golang continue keyword
last modified May 7, 2025
This tutorial explains how to use the continue
keyword in Go. We'll
cover loop control basics with practical examples of skipping iterations.
The continue statement skips the current iteration of the innermost loop. It's used to bypass specific iterations when certain conditions are met.
In Go, continue
can be used in for loops and with labels for more
complex control flow. It helps optimize loops by avoiding unnecessary code.
Basic continue in a for loop
The simplest use of continue
skips an iteration when a condition
is met. This example demonstrates skipping odd numbers.
package main import "fmt" func main() { for i := 0; i < 10; i++ { if i%2 != 0 { continue } fmt.Println(i) } }
The loop prints only even numbers. When i
is odd, continue
skips the print statement and moves to the next iteration.
Continue in a nested loop
continue
only affects the innermost loop. This example shows how it
works in nested loop structures.
package main import "fmt" func main() { for i := 0; i < 3; i++ { fmt.Printf("Outer loop iteration %d\n", i) for j := 0; j < 5; j++ { if j == 2 { continue } fmt.Printf(" Inner loop iteration %d\n", j) } } }
The inner loop skips iteration when j
equals 2, but the outer loop
continues normally. The continue only affects the loop where it's placed.
Labeled continue statement
Go supports labeled continues to control outer loops. This powerful feature helps manage complex nested loops.
package main import "fmt" func main() { OuterLoop: for i := 0; i < 5; i++ { for j := 0; j < 5; j++ { if i == 2 && j == 2 { continue OuterLoop } fmt.Printf("i=%d, j=%d\n", i, j) } } }
The label OuterLoop:
marks the outer loop. When executed,
continue OuterLoop
skips the current outer loop iteration.
Continue in a for-range loop
continue
works similarly in range loops. This example processes a
slice while skipping specific elements.
package main import "fmt" func main() { fruits := []string{"apple", "banana", "cherry", "date", "elderberry"} for index, fruit := range fruits { if len(fruit) < 5 { continue } fmt.Printf("%d: %s\n", index, fruit) } }
The loop skips fruits with names shorter than 5 characters. The continue
bypasses the print statement for these elements.
Practical example: Data filtering
This practical example demonstrates using continue
to filter out
invalid data points from processing.
package main import "fmt" func main() { temperatures := []float64{22.5, -999, 18.3, -999, 25.7, 19.2} validCount := 0 for _, temp := range temperatures { if temp == -999 { continue } fmt.Printf("Processing temperature: %.1f\n", temp) validCount++ } fmt.Printf("Processed %d valid temperatures\n", validCount) }
The loop skips invalid temperature readings (-999). The continue
statement helps process only valid data points efficiently.
Continue with complex conditions
This example shows continue
with more complex logical conditions
to demonstrate its flexibility.
package main import "fmt" func main() { for i := 1; i <= 20; i++ { if i%3 == 0 || i%5 == 0 { continue } fmt.Printf("%d ", i) } fmt.Println() }
The loop skips numbers divisible by 3 or 5. The continue
statement
works with any boolean condition to control loop flow.
Continue vs break vs return
This example contrasts continue
with other control flow statements
to clarify their differences.
package main import "fmt" func main() { fmt.Println("Continue example:") for i := 0; i < 5; i++ { if i == 3 { continue } fmt.Println(i) } fmt.Println("\nBreak example:") for i := 0; i < 5; i++ { if i == 3 { break } fmt.Println(i) } fmt.Println("\nReturn example:") for i := 0; i < 5; i++ { if i == 3 { return } fmt.Println(i) } fmt.Println("This won't print due to return") }
continue
skips to next iteration, break
exits the loop,
and return
exits the entire function. Each serves different purposes.
Source
This tutorial covered the continue
keyword in Go with practical
examples of loop control in various scenarios.
Author
List all Golang tutorials.