Python

Python Continue

“In Python, loops are used to repeatedly iterate over or execute a command. Python has a few control statements to assist us in managing the direction of these loops. Let’s say you want to end the loop when a certain condition is met or skip a specific iteration. By employing Python break, continue, and other control commands, Python enables us to carry out these operations. This brief tutorial examines the Python continue statement, how it affects the behavior of a loop, and how you can apply it to good purpose in your programs.”

What is Python Continue Statement?

Python’s continue statement stops the flow but restarts it at the loop’s beginning. For a specific iteration, the remaining code is not processed, and the flow is restarted for the following iteration. To skip a specific iteration, use the continue command in Python. Continue statement is also used when a condition is met, much like Python break.

A loop’s continue statement is used to advance to the subsequent iteration. The continue statement prevents the execution of any additional code. A continue statement does not entirely end a loop, in contrast to a break statement. When a condition is satisfied, you can skip over a section of a loop in Python by using the continue statement. The remainder of the loop will then continue to execute. Within loops, continue statements are typically used after an if statement.

Let’s now discuss these ideas using a few simple examples so that you can more easily identify the important ideas.

Example 1

You can utilize the continue statement both inside of a for loop and a while loop. The for loop is used in the example that follows to display even integers from 0 to 9. First, we demonstrated how to use a for loop and the range() method to traverse through a range of numbers from 0 to 9. The range() function returns a series of numbers that, by default, starts at 0 and increments by 1 before stopping before a given number.

Please note that the num%2 returns 0 when an index is an integer number; otherwise, it returns 1.

for num in range(10):
    if num % 2:
        continue
    print(num)

This is the output screen, where you can see that even numbers are shown.

Example 2

This example program will show you how to display odd numbers between 0 and 7 by using the continue statement. In a while loop, the continue statement will be utilized. To start, we’ve set the counter variable’s initial value to zero (counter num in the code). Second, the loop has been initiated if the counter num is less than 8. Third, for each iteration of the loop, we have raised the counter num by one. The code will skip the current iteration if the counter_num is even. If not, show the counter on the screen.

counter_num = 0
while counter_num < 8:
    counter_num += 1
    if not counter_num % 2:
        continue
    print(counter_num)

Here, you can see that when the program with the while loop and the continue statement is run, the odd integers are displayed.

Example 3

This example will discuss a continue statement used in a for loop. We want to create a program that only returns numbers from 10 to 17, but not 13. Assume that a “for” loop is necessary to complete this. The continue keyword is useful at this point. We’ll run a loop from 10 to 17 times while checking whether the iterator equals 13. The loop will print the result if it equals 13; else, we’ll use the continue statement to skip to the next iteration and display any results. A sample of the aforementioned situation is shown in the code below:

for num in range(10, 17):  
    if num == 13:  
        continue  
    print( num )

The numbers from 10 to 16 are displayed in the screenshot below, with the exception of 13 (which is shown as it is in the code above).

Example 4

To demonstrate how the Python continue statement functions, let’s use an example. In the example that follows, we omit printing the letter “e” in our string by using a continue statement, after which we continue iterating.

Notice that we are looping through a string of names. If the letter “e” appears in the given string, the for-loop’s condition instructs us to skip that iteration and move on to the next one. When an external condition is met, our continue statement is put into action. This program’s version of the condition is “string == e.” When the string equals “e”, our program terminates that loop iteration.

This is how the entire piece of code looks.

for string in "Alexander":
    if string == "e":
        continue
    print(string)

You’ll see that the continue statement skipped that iteration, and the letter “e” was not written to the console.

Example 5

Take into account the scenario in which you must create a program that outputs the numbers from 1 to 15 but not 6. Let’s suppose that this task should be done using a loop, and only one loop may be used. And here, the continue statement is used. What we can do is execute a loop from 1 to 15 times, comparing the value of the iterator each time with 6. We will employ the continue statement to move on to the next iteration if the value is equal to 6; otherwise, we will print the value.

Due to the condition stated in the code, all of the numbers defined in the output are displayed instead of the number 6, as can be seen in this output.

Conclusion

In the present iteration of the loop, all outstanding statements are rejected using the continue statement. In essence, this statement gives the first line of the loop back control. This concept has been thoroughly covered in this article, along with some concise examples. Five unique examples are explained for this concept in this guide.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content