Python continue Statement

The continue statement in Python is used to skip the current iteration of a loop (for loop or while loop) and move to the next iteration without executing the remaining code in the current iteration.

When the continue statement is encountered in a loop, the program immediately jumps to the next iteration of the loop, ignoring any remaining statements in the current iteration.

Here’s an example that uses continue in a for loop to skip printing even numbers:

for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

In this example, the loop starts with i = 0, which is an even number. The if statement checks if i is divisible by 2 with no remainder, which is true for even numbers. Therefore, the continue statement is executed, and the loop immediately moves on to the next iteration without executing the print(i) statement. This process repeats for all even numbers in the loop, so only odd numbers are printed.

The output of this program is:

1
3
5
7
9

Note that if the continue statement is executed in the last iteration of a loop, the loop ends immediately, and the remaining statements in the loop are not executed.

Example of Python Continue Statements in For Loop:

Here is an example of using continue statement in a for loop in Python to skip certain elements:

# Create a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Loop through the numbers
for num in numbers:
    # If the number is even, skip to the next iteration
    if num % 2 == 0:
        continue
    
    # If the number is odd, print it
    print(num)

In this example, we have a list of numbers from 1 to 10. We loop through each number, and if a number is even, we use the continue statement to skip it and move to the next iteration. If the number is odd, we print it.

The output of this program is:

1
3
5
7
9

As we can see, the even numbers (2, 4, 6, 8, and 10) are skipped, and only the odd numbers (1, 3, 5, 7, and 9) are printed.

Python Continue vs. Pass:

Both continue and pass are control flow statements in Python, but they serve different purposes.

continue is used inside loops (for loop or while loop) to skip the current iteration and move on to the next iteration. It is typically used when we want to skip certain elements in a loop based on some condition. The code inside the loop after the continue statement is not executed for the current iteration.

For example, let’s say we have a list of numbers and we want to print only the even numbers in the list:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for num in numbers:
    if num % 2 != 0:
        continue
    print(num)

In this example, we use continue to skip the odd numbers and print only the even numbers.

On the other hand, pass is used as a placeholder for code that has not been implemented yet. It is used when we want to define a function, class, or control statement but don’t want to add any functionality to it yet.

For example, let’s say we want to define a function that prints the name of a person:

def print_name(name):
    # TODO: Add code here
    pass

In this example, we use pass as a placeholder for the code that we haven’t implemented yet. We can define the function structure and add the code later.

In summary, continue is used to skip a specific iteration in a loop, while pass is used as a placeholder for code that has not been implemented yet.

In this example, we use pass as a placeholder for the code that we haven’t implemented yet. We can define the function structure and add the code later.

In summary, continue is used to skip a specific iteration in a loop, while pass is used as a placeholder for code that has not been implemented yet.