Python continue Statement

Introduction

The continue statement in Python is used to skip the current iteration of a loop and proceed to the next iteration. This can be useful when you want to skip certain elements in a sequence or when a particular condition is met. The continue statement can be used in both for and while loops to provide more control over the flow of the program.

Syntax

The continue statement can be used inside for or while loops. When the continue statement is executed, the loop immediately jumps to the next iteration, skipping any code that follows it within the loop for the current iteration.

for variable in sequence:
    if condition:
        continue
    # block of code

while condition:
    if condition:
        continue
    # block of code

Using continue in a for Loop

The continue statement can be used to skip certain elements in a sequence.

Example

fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
    if fruit == "banana":
        continue
    print(fruit)

Output:

apple
cherry
date

In this example, the loop skips printing "banana" and continues with the next iterations.

Using continue in a while Loop

The continue statement can also be used to skip certain iterations in a while loop.

Example

i = 0
while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)

Output:

1
2
4
5

In this example, the loop skips printing the number 3 and continues with the next iterations.

Practical Examples Using continue

Example 1: Skipping Even Numbers

This example demonstrates how to use the continue statement to skip even numbers in a loop.

for i in range(1, 11):
    if i % 2 == 0:
        continue
    print(i)

Output:

1
3
5
7
9

Example 2: Skipping Elements in a List

This example shows how to skip certain elements in a list based on a condition.

numbers = [1, -2, 3, -4, 5, -6]
for num in numbers:
    if num < 0:
        continue
    print(num)

Output:

1
3
5

Example 3: Filtering User Input

This example uses the continue statement to skip invalid user inputs and only process valid ones.

valid_inputs = []
while True:
    user_input = input("Enter a positive number (or 'stop' to end): ")
    if user_input.lower() == 'stop':
        break
    if not user_input.isdigit() or int(user_input) <= 0:
        print("Invalid input. Please try again.")
        continue
    valid_inputs.append(int(user_input))

print("Valid inputs:", valid_inputs)

Output (example interaction):

Enter a positive number (or 'stop' to end): -1
Invalid input. Please try again.
Enter a positive number (or 'stop' to end): abc
Invalid input. Please try again.
Enter a positive number (or 'stop' to end): 5
Enter a positive number (or 'stop' to end): 10
Enter a positive number (or 'stop' to end): stop
Valid inputs: [5, 10]

Example 4: Printing Characters Except Vowels

This example uses the continue statement to skip vowels while printing characters from a string.

text = "Hello, World!"
vowels = "aeiouAEIOU"

for char in text:
    if char in vowels:
        continue
    print(char, end="")

Output:

Hll, Wrld!

Conclusion

The continue statement is used for controlling the flow of loops in Python. It allows you to skip the current iteration and proceed to the next one, which can be useful in various scenarios where certain conditions need to be met. By using continue in both for and while loops, you can handle different cases where you need to skip specific iterations. The provided examples demonstrate practical uses of the continue statement in different contexts.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top