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.
for _, line := range lines { if line == "" { continue // skip blank lines } process(line) }
Try it
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.
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
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
continueoutside aforloop is a compile error. Inside aswitchorselectthat is itself inside a loop,continuestill applies to the loop, unlikebreak, which would leave the switch. - Post statement runs. In
for i := 0; i < n; i++, acontinuejumps toi++and then re-checksi < 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.
continuecopies the value into the next iteration's variable before the post statement runs. - Guard clauses. Using
continueearly keeps the main part of a loop body unindented, which many Go programs prefer to deepifnesting. - 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,
continuesimply returnstruefrom the generatedyieldcall. - Closures.
continuecannot cross a function boundary.