Python : Continue keyword in Loops

To manage looping specifications, the Python programming language contains the following types of loops. The loops can be executed in three different ways in Python. Although all of the methods have similar basic features, they vary in syntax and the time it takes to search for conditions.

For loops:

In Python, the for loop is used to iterate over a list, tuple, or string, or other iterable properties. Traversal is the process of iterating over a sequence.

While loops:

While Loops are used in Python to execute a set of statements over and over until a condition is met.. When the condition is met, the line immediately following the loop in the programme is executed. Indefinite iteration is demonstrated by the while loop. The term “indefinite iteration” refers to the fact that the number of times the loop is executed is not specified explicitly in advance.

Continue keyword in Python Loops

1)Continue keyword

In Python, loops are used to simplify and repeat tasks in an effective manner. However, there can be occasions when you want to completely exit the loop, miss an iteration, or ignore the condition. Loop control statements can be used to do this. Control statements in loops alter the execution sequence.

Like the break comment, continue is a loop control statement. The continue statement is the polar opposite of the break statement in that it pushes the loop to perform the next iteration rather than ending it.
The continue argument, as its name implies, forces the loop to continue or perform the next iteration. When the continue statement is used in a loop, the code within the loop that follows the continue statement is skipped, and the loop’s next iteration begins.

2)Continue keyword in While loop

The control will jump back to the beginning of the while loop if the continue keyword is used within the loop. For that iteration, all lines following the continue keyword will be skipped.

Consider the following scenario: we want to print all numbers from 1 to n that are not divisible by 5.

Below is the implementation:

# given n
n = 16
# let us take a temp variable to 0 as we need to print all numbers from 1 to n
temp = 0
while(temp < n):
    # increment the temp
    temp += 1
    # if the temp is divisible by 5 then use continue
    # As a result, the number will not be printed.
    if(temp % 5 == 0):
        continue
    else:
        print(temp)

Output:

1
2
3
4
6
7
8
9
11
12
13
14
16

Explanation:

We are printing numbers from 1 to n in this while loop. However, within the loop body, we have a check that says if temp is divisible by 5 we should execute the continue keyword.

As a result, when the value of temp is divisible by 5, the continue statement is executed. It returns control to the beginning of the loop and skips the print statement at the end of the loop body.

3)Continue keyword in For loop

Let the implement the above code using for loop.

Below is the implementation:

# given n
n = 16
# using for loop
for temp in range(1, n+1):
    # if the temp is divisible by 5 then use continue keyword
    # As a result, the number will not be printed.
    if temp % 5 == 0:
        continue
    else:
        print(temp)

Output:

1
2
3
4
6
7
8
9
11
12
13
14
16

Related Programs: