The continue statement helps you skip the rest of the current loop step and move to the next one. It makes your loops cleaner when you want to ignore some cases.
0
0
Continue statement in Go
Introduction
When you want to skip processing certain items in a list but keep looping over the rest.
When checking conditions inside a loop and you want to jump to the next item if a condition is met.
When filtering data inside a loop without breaking the entire loop.
When you want to avoid nested if-else blocks by skipping unwanted cases early.
Syntax
Go
continueThe continue statement is used inside loops like for.
It immediately skips to the next iteration of the loop.
Examples
This loop prints numbers 1 to 5 but skips printing 3.
Go
for i := 1; i <= 5; i++ { if i == 3 { continue } fmt.Println(i) }
This loop prints all letters in "hello" except the letter 'l'.
Go
for _, ch := range "hello" { if ch == 'l' { continue } fmt.Printf("%c", ch) }
Sample Program
This program loops from 1 to 10. It skips even numbers using continue and prints only odd numbers.
Go
package main import "fmt" func main() { for i := 1; i <= 10; i++ { if i%2 == 0 { continue // skip even numbers } fmt.Println(i) // print only odd numbers } }
OutputSuccess
Important Notes
Using continue can make your loops easier to read by avoiding deep nesting.
Be careful not to create infinite loops by skipping the part that changes the loop variable.
Summary
continue skips the rest of the current loop step and moves to the next.
It is useful to ignore certain cases inside loops without stopping the whole loop.
Use it to keep your code simple and clear when filtering or checking conditions.