Python While Loop with Continue Statement
Python While Loop repeats a block of code while its condition remains true. The Python continue statement lets you skip the remaining statements in the current iteration and move on to the next condition check.
This is useful when some values should be ignored without ending the loop. In a while loop, you must also make sure that variables controlling the loop are updated before a continue statement when required; otherwise, the loop can repeat indefinitely.
In nested loops, a continue statement affects only the nearest enclosing loop. If it is inside the inner while loop, Python skips the rest of that inner-loop iteration, not the outer loop.
Python while loop continue syntax
The following pattern shows a continue statement inside a Python while loop.
#statement(s)
while condition :
#statement(s)
if continue_condition :
continue
#statement(s)
When continue_condition is true, Python executes continue. Statements that appear later in the current loop body are skipped, and execution returns to the while condition.
Following is the flow-diagram of while loop with continue statement.
How continue changes the flow of a Python while loop
- Python checks the
whilecondition. - If the condition is true, the loop body starts.
- Statements before
continuerun normally. - If Python reaches
continue, it skips the rest of the current loop body. - Python checks the
whilecondition again before starting another iteration.
The placement of continue therefore matters. Code before it can update counters or state, while code after it will not run for iterations in which continue is reached.
Python while loop continue example: print only even numbers
In this example, we shall write a Python program with while loop to print numbers from 1 to 20. But, when we get an odd number, we will continue the loop with next iterations.
Python while loop program that skips odd numbers
i = 0
while i <= 20 :
i += 1
if i % 2 == 1 :
continue
print(i)
Make sure that you update the control variables participating in the while loop condition, before you execute the continue statement.
Here, i is incremented before the odd-number check. When i is odd, continue skips print(i). When i is even, the print statement runs.
Output
2
4
6
8
10
12
14
16
18
20
Why the while-loop counter should be updated before continue
A for loop advances to its next item automatically, but a while loop usually depends on a variable that your code changes explicitly. If continue is reached before that update, the controlling value may never change and the loop condition may stay true forever.
A safe pattern is to update the loop-control variable before any branch that can execute continue, as in the following example.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
1
2
4
5
When i becomes 3, Python skips the print statement. Because i was already incremented, the next condition check proceeds with a new value.
Using continue with an if statement inside a while loop
continue is commonly placed inside an if statement so that only selected iterations are skipped. The if statement decides whether the skip should happen; continue itself controls the loop.
For example, the following loop ignores empty strings while processing a list of values.
values = ["10", "", "25", "", "40"]
index = 0
while index < len(values):
value = values[index]
index += 1
if value == "":
continue
print(int(value))
10
25
40
The empty strings are skipped, but the loop continues processing the remaining items.
Continue in a nested Python while loop
In this example, we shall write a Python program with an nested while loop. We will use continue statement in the body of inner while loop based on some condition.
Python nested while loop program with continue
i = 1
while i < 6 :
j = 1
while j < 8 :
if j % 2 == 1 :
j += 1
continue
print(i*j, end=" ")
j += 1
print()
i += 1
Output
2 4 6
4 8 12
6 12 18
8 16 24
10 20 30
Here, continue belongs to the inner loop controlled by j. When j is odd, the rest of that inner-loop iteration is skipped. The outer loop controlled by i continues normally.
Python continue vs break vs pass in while loops
continue, break, and pass have different purposes. They should not be used interchangeably.
| Statement | Effect in a while loop |
|---|---|
continue | Skips the remaining statements in the current iteration and checks the loop condition again. |
break | Stops the loop completely and continues with the first statement after the loop. |
pass | Does nothing. Execution continues with the next statement in the same block. |
A continue statement must be inside a loop. It is often nested inside an if statement, but the surrounding loop is what makes continue valid.
Common mistakes with continue in a Python while loop
- Forgetting to update the loop-control variable: the same condition can remain true and cause an infinite loop.
- Expecting code after continue to run: statements later in the current iteration are skipped.
- Using continue outside a loop:
continueis loop-control syntax and cannot be used as a general replacement foriflogic. - Assuming continue exits a nested loop: it affects only the nearest enclosing loop. Use different logic when you need to control an outer loop.
- Confusing continue with pass:
passis a no-operation placeholder; it does not skip the rest of an iteration.
Key points for Python while loop continue
Use continue in a Python while loop when the current iteration should stop early but the loop itself should keep running. Python skips the statements that follow continue in that iteration and checks the while condition again. In this Python Tutorial, we learned how to use continue statement in Python While Loop.
TutorialKart.com