ProgramGuru

continue Statement

Start with Go continue Statement

A continue statement skips the remainder of the current loop iteration and starts the next iteration. Unlike break, it does not exit the loop.

What you’ll learn

  • Filter repeated work with continue.
  • Compare continue with break.
  • Avoid progress problems in condition-only loops.

Understand the idea

Use continue when some values should be ignored, such as invalid records or numbers that do not satisfy a filter. In a three-part for loop, the post statement still executes before the next condition check.

In nested loops, an unlabeled continue applies to the innermost enclosing loop. Prefer simple conditions when they communicate the same idea more clearly than a complex sequence of continues.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for continueA condition branches into true or false paths before continuing.number is odd?TRUEFALSESkip this iterationPrint even numberMove to the next value
Trace control flow in the order shown, then compare it with the program.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	for number := 1; number <= 6; number++ {
		if number%2 != 0 {
			continue
		}
		fmt.Println("Even:", number)
	}
	fmt.Println("Done")
}

Output

Even: 2
Even: 4
Even: 6
Done

Trace what the program does

Odd numbers have a nonzero remainder when divided by 2, so the loop skips their print statement. Even numbers 2, 4 and 6 are printed, and the loop ends normally with Done.

Avoid common mistakes

  • continue does not mean stop the loop; use break for that.
  • Make sure a condition-only loop still advances its state when you use continue, or you may create an infinite loop.

Try it yourself

Change the condition to skip multiples of 3 instead. List the remaining numbers before running.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What does continue do in a loop?
Choose one answer. You can try again.

Answer and explanation

Skip the rest of the current iteration

Correct. Odd numbers have a nonzero remainder when divided by 2, so the loop skips their print statement.

What you should remember

You have practiced Go continue Statement in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Variable Scope →