3. Continue

continue is a statement that skips the rest of the current pass and jumps back to the loop condition:

The loop still runs five times. When n is 3, continue skips print(n) and the next pass starts.

  • In a while loop, update the counter before continue. If the update is after it, that pass never reaches the update and the loop can run forever.
  • continue does not end the loop; only break does.
#01

Skip one number

Start n at 0. While n is less than 5, add 1 first, then continue when n is 3; otherwise print n.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Skip even numbers

Start n at 0. While n is less than 7, add 1, then continue when n is even (n % 2 == 0); otherwise print n.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Skip two values

Start n at 0. While n is less than 6, add 1, then continue when n is 2 or n is 5; otherwise print n.

Print exactly:

Create a free account to save your progress and unlock chapter 2.