Decreasing loop with continue

The loop in Listing 1 uses continue before its print statement.

Listing 1: A decreasing for loop with continue (Question 1).
for i in range(4, 0, -2):
    # Decide whether to display the current value.
    if i % 2 == 0:
        continue

    print(i)

What does the code print?

  1. 4, 2, 0
  2. 4, 2, 0, -2
  3. Nothing is printed.
  4. 4, 2, 0, -2, -4

The continue statement skips the rest of the current loop iteration and moves to the next value.

  • range(start, stop, step) supplies integers from start toward, but excluding, stop.
    • step defaults to 1, and range(stop) starts at 0.
    • list(range(2, 7, 2)) is [2, 4, 6]; list(range(5, 1, -2)) is [5, 3].
    • A range is empty when the step cannot reach the stop; a zero step raises ValueError.
  • list collects the supplied values in a new list.