In this Python tutorial, you will learn how the continue statement skips the remaining code in the current loop iteration and proceeds with the next iteration. The examples cover for loops, while loops, nested loops, filtering values, and common mistakes.

What Is the continue Statement in Python?

The Python continue statement is a loop-control statement that skips the rest of the current iteration.

When Python executes continue, it does not terminate the loop. Instead, it moves directly to the next iteration of the nearest enclosing for or while loop.

Statements placed after continue in the same loop iteration are not executed. The loop then checks or retrieves the next value according to the type of loop.

The continue statement can be used only inside a loop. Using it outside a for or while loop causes a SyntaxError.

Python continue Statement Syntax

Following is the syntax of Python continue statement.

</>
Copy
 continue

The statement commonly appears inside an if condition so that only selected iterations are skipped.

</>
Copy
for item in iterable:
    if condition:
        continue
    # Statements here are skipped when condition is true

Python continue in a For Loop

In the following example, we will use continue statement inside Python For loop to skip the statements inside the loop for this iterationand continue with the rest of iterations.

Python Program

</>
Copy
for number in [1, 2, 3, 5, 6, 10, 11, 12]:
	if number==6:
		continue
	print(number)
	
print('Bye')

Output

1
2
3
5
10
11
12
Bye

When number is 6, Python executes continue. The print(number) statement is skipped for that iteration, so 6 does not appear in the output. The loop then continues with 10.

Python continue in a While Loop

In the following example, we will use continue statement inside Python While Loop.

Python Program

</>
Copy
i=1
while i < 11:
	if i==6:
		i=i+1
		continue
	print(i)
	i=i+1
	
print('Bye')

Output

1
2
3
4
5
7
8
9
10
Bye

When i becomes 6, the program increments i before executing continue. The print statement is skipped for that iteration, and the loop resumes with i equal to 7.

Updating the loop-control variable before continue is important in this example. If the increment were omitted, i would remain 6, the same condition would remain true, and the loop would continue indefinitely.

Using Python continue to Skip Selected Values

A common use of continue is to filter values during iteration. The following program skips negative numbers and prints only values that are zero or greater.

</>
Copy
numbers = [8, -3, 5, -1, 0, 12]

for number in numbers:
    if number < 0:
        continue
    print(number)

Output

8
5
0
12

For each negative value, continue skips the print statement. The loop still processes every remaining item in the list.

Skipping Even Numbers with continue

The next example uses the remainder operator to skip even numbers and print only odd numbers.

</>
Copy
for number in range(1, 8):
    if number % 2 == 0:
        continue
    print(number)

Output

1
3
5
7

Whenever the remainder after division by 2 is zero, the number is even and the current iteration is skipped.

How continue Works in Nested Python Loops

In nested loops, continue affects only the nearest enclosing loop. It skips the remainder of the current iteration of that loop, while the outer loop continues normally.

</>
Copy
for row in range(1, 4):
    for column in range(1, 4):
        if column == 2:
            continue
        print(row, column)

Output

1 1
1 3
2 1
2 3
3 1
3 3

When column equals 2, the inner loop skips the print statement. The outer loop is not skipped or terminated.

Python continue with a Loop else Block

A loop’s else block still runs when iterations are skipped with continue, provided the loop eventually finishes normally. Unlike break, continue does not terminate the loop.

</>
Copy
for number in range(1, 5):
    if number == 2:
        continue
    print(number)
else:
    print('Loop completed')

Output

1
3
4
Loop completed

The iteration for 2 is skipped, but the loop completes all remaining iterations. Therefore, the else block executes.

Difference Between continue, break, and pass

  • continue skips the remaining statements in the current iteration and proceeds to the next iteration.
  • break terminates the nearest enclosing loop completely.
  • pass performs no operation and is generally used as a placeholder where Python requires a statement.

Use continue when only certain iterations should be ignored. Use break when the loop should stop entirely.

SyntaxError When continue Is Used Outside a Loop

In the following example, we will try to use continue statement outside for or while loop statement.

Python Program

</>
Copy
i=1

if i==2:
	continue
	
print('Bye')

Output

  File "example1.py", line 4
    continue
    ^
SyntaxError: 'continue' not properly in loop

You will get SyntaxError with the message 'continue' not properly in loop if you try to use continue statement outside for or while loop.

An if statement does not by itself provide a valid context for continue. The statement must be enclosed by an active loop.

Common Python continue Statement Mistakes

  • Using continue outside a loop: It is valid only inside a for or while loop.
  • Forgetting to update a while-loop variable: If the update is placed after continue, it may never execute, causing an infinite loop.
  • Expecting continue to end the loop: It skips only the current iteration; use break to terminate the loop.
  • Placing required work after continue: Statements later in the same iteration are skipped whenever continue executes.
  • Expecting an outer loop to be skipped: In nested loops, continue applies only to the nearest enclosing loop.

Python continue Statement FAQs

What does continue do in Python?

The continue statement skips the remaining code in the current loop iteration and starts the next iteration.

Can continue be used in both for and while loops?

Yes. Python allows continue in both for loops and while loops.

Can continue cause an infinite while loop?

Yes. If continue skips the statement that updates the loop-control variable, the loop condition may remain true indefinitely.

Does continue skip all nested loops?

No. It affects only the nearest enclosing loop and only its current iteration.

Summary of Python continue

The Python continue statement skips the rest of the current iteration without terminating the loop. It can be used in for loops and while loops to ignore selected values or conditions while allowing later iterations to run.

In this Python Tutorial, we learned how to use Python continue statement with for and while loops.