VMware ESXi and vSphere Cluster Management

Python continue Statement: Skip the Current Loop Iteration

Learn how Python's continue statement skips the rest of the current loop iteration while allowing a for or while loop to continue.

The Python continue statement is a loop-control statement. It immediately ends the current iteration, or one pass through a loop body, and starts the next iteration.

A loop remains active after continue. In a for loop, Python selects the next item from the iterable. In a while loop, Python checks the loop condition again.

How continue changes loop execution

The loop body is the indented code that runs during each iteration. Without continue, execution normally proceeds from the first statement in the body to the last:

for item in items:
    first_step(item)
    second_step(item)
    third_step(item)

If execution reaches continue, all later statements in that iteration are bypassed:

for item in items:
    first_step(item)
    continue
    second_step(item)  # Not reached
    third_step(item)   # Not reached

Here, first_step(item) has already run. The continue statement then moves control directly to the next iteration, so the statements below it do not run for that item.

Using continue with a condition

continue is commonly placed inside an if statement. An if statement is a conditional statement: it runs its indented code only when its condition is true.

for value in values:
    if should_ignore(value):
        continue

    process(value)

When should_ignore(value) is true, continue skips process(value). When the condition is false, continue does not run, so the loop proceeds to the remaining loop-body work.

Flow summary

Current loop valueCondition resultDoes continue run?Are later statements in this iteration executed?Next action
Value to ignoreTrueYesNoStart the next iteration
Value to processFalseNoYesFinish the body, then continue normally

Example: skip a specific number

The following for loop uses range, a built-in object commonly used to generate integer values for iteration. The range goes from 1 through 10 because the ending value 11 is not included.

for number in range(1, 11):
    if number == 5:
        continue
    print(number)

Output:

1
2
3
4
6
7
8
9
10

When number equals 5, the equality condition is true. Python executes continue and does not execute print(number) for that iteration. The loop then selects 6 and processes the remaining numbers.

Example: omit vowels from text

A string can be iterated character by character. In this example, a vowel means one of the lowercase letters a, e, i, o, or u.

text = input("Enter a word or phrase: ")

for character in text:
    if character in "aeiou":
        continue
    print(character, end="")

For input such as Hello, world!, the output is:

Hll, wrld!

The lowercase vowels are skipped. Consonants are printed, and spaces and punctuation are also printed because they do not match the vowel condition.

This version omits lowercase vowels only. To handle uppercase vowels as well, normalize each character before testing:

for character in text:
    if character.lower() in "aeiou":
        continue
    print(character, end="")

Practical pattern: skip invalid records

continue is useful when a loop should ignore blank or invalid values before reaching its main processing code.

records = ["A12", "", "B07", None, "C31"]

for record in records:
    if not record:
        continue
    print("Processing", record)

Only nonblank records reach the processing statement. The empty string and None are skipped, while the valid record values are processed.

continue versus break

break is another loop-control statement, but it exits the current loop completely. It prevents all later iterations. continue skips only the current iteration and allows later iterations to occur.

StatementAllowed contextEffect on current iterationEffect on remaining loop iterationsTypical use
continueInside a for or while loopStops the current iteration immediatelyLater iterations continueIgnore one item or case
breakInside a for or while loopStops the loop immediatelyNo later iterations occurStop after finding a result or reaching a limit
passWhere a statement is syntactically requiredDoes nothing; execution continues to the next statementThe loop proceeds normallyUse a temporary no-operation placeholder

Side-by-side example

# continue: prints 1, 3, 4
for number in range(1, 5):
    if number == 2:
        continue
    print(number)

# break: prints 1, then ends the loop
for number in range(1, 5):
    if number == 2:
        break
    print(number)

With continue, only 2 is omitted. With break, the loop ends as soon as it reaches 2, so 3 and 4 are never considered.

continue versus pass

pass is a no-operation placeholder. It performs no action and does not change loop control flow.

for number in range(3):
    if number == 1:
        pass
    print(number)

This prints 0, 1, and 2 because execution continues from pass to print(number). Replacing pass with continue would skip the print statement when number is 1.

Placement and limitations

continue is valid only inside a for or while loop. Using it outside a loop produces a SyntaxError.

continue  # SyntaxError: not inside a loop

Remember the order of execution:

  • Statements before continue have already executed.
  • The condition controlling continue must be true for it to run.
  • Statements after continue in the same iteration are bypassed.
  • The loop then proceeds to its next iteration or checks its condition again.

continue in nested loops

A nested loop is a loop inside another loop. A continue statement affects only the nearest, or innermost, enclosing loop.

for row in range(1, 3):
    for column in range(1, 4):
        if column == 2:
            continue
        print("row", row, "column", column)

The output is:

row 1 column 1
row 1 column 3
row 2 column 1
row 2 column 3

Column 2 is skipped for each row, but the outer loop continues from row 1 to row 2. If outer-loop control is required, the loops may need restructuring, a flag, or a function that can return control to the caller.

Using continue safely in a while loop

A while loop repeats while its condition is true. It must make progress toward becoming false. A common error is putting the counter update after continue:

number = 0
while number < 5:
    if number == 2:
        continue
    number += 1

When number becomes 2, continue runs before the increment. The value remains 2 forever, so the loop never reaches its stopping condition.

Update the loop-control variable before the possible skip:

number = 0
while number < 5:
    number += 1
    if number == 2:
        continue
    print(number)

This loop prints 1, 3, 4, and 5. The counter increases on every iteration, including the iteration that skips 2, so the loop terminates safely.

Troubleshooting continue

Code after continue never runs

This is intentional for values whose condition is true. Move work that must always happen before continue, or change the conditional structure.

The loop stops instead of skipping a value

You may have used break. Use continue when later items should still be processed.

A SyntaxError occurs

Check that continue is indented inside a for or while loop. It cannot appear at the top level of a program or inside a function that is not itself inside a loop.

A while loop runs forever

The update to the loop-control variable may be after continue. Move that update before the possible skip, or redesign the loop so every path changes the state used by its condition.

Only lowercase vowels are removed

Use character.lower() before testing, as shown in the text example, or include uppercase vowels in the condition.

continue affects only one nested-loop level

This is normal: Python applies continue to the innermost enclosing loop. Restructure the loops or use another control pattern when the outer loop must also change behavior.

Key points

  • continue immediately ends the current loop iteration.
  • The loop remains active and proceeds to its next iteration.
  • It can be used in both for and while loops.
  • It is often placed inside an if statement to filter unwanted values.
  • Code before continue runs; code after it in that iteration does not.
  • break ends the entire loop, while pass does nothing.
  • In nested loops, continue affects only the innermost loop.
  • In a while loop, ensure every path still advances toward the stopping condition.

For a focused reference, see the Python continue statement.