While Loop with Continue
3 min read ·
The
continue statement is used to skip the current iteration of a loop and move directly to the next iteration.When Python encounters
continue:- The remaining code inside the loop is skipped
- Control goes back to the loop condition
Basic Syntax of continue in while Loop
continueis written inside the loop- It does not stop the loop
- It skips only the current iteration
Simple Example of while with continue
- When
iis 3, print is skipped - Loop continues with next value
Output:
1
2
4
5
continue with Even Numbers
- Odd numbers are skipped
- Only even numbers are printed
continue with User Input
0is skipped- Negative number stops the loop
continue with String Input
- Empty input is skipped
- Loop continues until
"exit"
continue with Condition Logic
continue vs Normal Execution
Without continue
With continue
Common Mistake with continue
Caution
This creates an infinite loop because i is not updated before continue.
Correct Way to Use continue
continue inside Nested Conditions
Real World Example
Real World Scenario
Skipping invalid inputs and continuing program execution
Exercise
Practice Task
- Print numbers from 1 to 10 but skip 5
- Skip even numbers and print only odd numbers
- Ignore empty user input using
continue - Use
continuecorrectly without creating infinite loop