Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python continue Statement

The continue statement is a control flow statement in Python that allows you to skip the rest of the current iteration in a loop (i.e., for or while loop) and proceed to the next iteration. The continue statement is often used in situations where you want to skip specific elements in a sequence and continue processing the remaining elements.

In this tutorial, we will discuss the usage of the continue statement in Python with examples.

  • Using continue in a for loop:

When the continue statement is encountered inside a for loop, the current iteration is terminated, and the loop proceeds with the next iteration.

Example:

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

Output:

1
2
4
5

In this example, the for loop iterates over the numbers from 1 to 5. When i is equal to 3, the continue statement is executed, and the current iteration is skipped. The numbers 1, 2, 4, and 5 are printed, but the number 3 is not.

  • Using continue in a while loop:

Similar to the for loop, the continue statement can be used inside a while loop to skip the current iteration and proceed to the next iteration.

Example:

counter = 1

while counter <= 5:
    if counter == 4:
        counter += 1
        continue
    print(counter)
    counter += 1

Output:

1
2
3
5

In this example, the while loop continues as long as counter is less than or equal to 5. When counter is equal to 4, the continue statement is executed, and the current iteration is skipped. The numbers 1, 2, 3, and 5 are printed, but the number 4 is not.

  • continue with nested loops:

When using nested loops (i.e., a loop inside another loop), the continue statement will only affect the innermost loop in which it is placed.

Example:

for i in range(1, 4):
    for j in range(1, 4):
        if j == 2:
            continue
        print(f"i: {i}, j: {j}")

Output:

i: 1, j: 1
i: 1, j: 3
i: 2, j: 1
i: 2, j: 3
i: 3, j: 1
i: 3, j: 3

In this example, the continue statement is inside the inner for loop. When j is equal to 2, the inner loop skips the current iteration, but the outer loop continues to execute. As a result, the output only shows the values of i and j when j is not equal to 2.

In conclusion, the continue statement is a control flow statement in Python that allows you to skip the rest of the current iteration in a loop and proceed to the next iteration. The continue statement is useful in situations where you want to skip specific elements in a sequence and continue processing the remaining elements.

  1. Skipping iterations with continue in Python:

    • Description: The continue statement is used to skip the rest of the code in the current iteration and move to the next iteration of the loop.
    • Code:
    for i in range(5):
        if i == 2:
            continue
        print(i)
    
  2. Conditional use of continue in Python loops:

    • Description: continue can be used conditionally to skip iterations based on certain conditions.
    • Code:
    numbers = [1, 2, 3, 4, 5]
    
    for num in numbers:
        if num % 2 == 0:
            continue
        print(num)
    
  3. Using continue with for loops in Python:

    • Description: continue is commonly used with for loops to skip iterations and move to the next one.
    • Code:
    for char in "Python":
        if char == "h":
            continue
        print(char)
    
  4. Breaking out of inner loops with continue:

    • Description: continue can be used to skip iterations in inner loops without exiting the entire loop.
    • Code:
    for i in range(3):
        for j in range(3):
            if j == 1:
                continue
            print(i, j)
    
  5. Common patterns for using continue in Python:

    • Description: continue is often used to skip iterations for specific conditions while processing elements in a loop.
    • Code:
    for number in range(10):
        if number % 3 == 0:
            continue
        print(number)
    
  6. continue statement vs break statement in Python:

    • Description: continue skips the rest of the code in the current iteration and moves to the next one, while break exits the entire loop.
    • Code:
    for i in range(5):
        if i == 2:
            continue
        print(i)
        if i == 3:
            break
    
  7. Handling specific conditions with continue in loops:

    • Description: continue can be used to handle specific conditions by skipping unnecessary code execution.
    • Code:
    for person in people:
        if person["age"] < 18:
            continue
        process_adult(person)
    
  8. Using continue with while loops in Python:

    • Description: continue can also be used with while loops to skip the rest of the code in the current iteration.
    • Code:
    count = 0
    
    while count < 5:
        count += 1
        if count == 3:
            continue
        print(count)