Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The continue statement

Reference for Go continue: skip the rest of a loop body and start the next iteration, with labeled continue for nested loops and the post statement rule.

continue stops the current iteration of the innermost enclosing for loop and begins the next one. In a three-part loop the post statement (such as i++) still runs, and then the condition is tested again. In a for ... range loop the next element is fetched.

continue only works in for loops. A label lets it target an outer loop, which is handy to move on to the next item of an outer collection from inside an inner loop.

Go
for _, line := range lines {
    if line == "" {
        continue // skip blank lines
    }
    process(line)
}

Try it

Go Skip unwanted values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Labeled continue

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
ContinueStmt = "continue" [ Label ] .
Part Description
Label Optional. Must name an enclosing for statement (not a switch or select) in the same function.

Usage notes

  • Only loops. A continue outside a for loop is a compile error. Inside a switch or select that is itself inside a loop, continue still applies to the loop, unlike break, which would leave the switch.
  • Post statement runs. In for i := 0; i < n; i++, a continue jumps to i++ and then re-checks i < n. A condition-only loop has no post statement, so make sure the state still changes, otherwise you create an infinite loop.
  • Per-iteration variables. Since Go 1.22 each iteration of a three-part loop has its own copy of the loop variable. continue copies the value into the next iteration's variable before the post statement runs.
  • Guard clauses. Using continue early keeps the main part of a loop body unindented, which many Go programs prefer to deep if nesting.
  • Labels must be used. An unused label is a compile error, and the label must be directly in front of the loop it names.
  • Range-over-func. In a loop over an iterator function, continue simply returns true from the generated yield call.
  • Closures. continue cannot cross a function boundary.