Python Continue Statement

Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

  • Continue statement
  • Break statement
  • Pass statement

In this article, the main focus will be on continue statement.

Continue statement

Continue is also a loop control statement just like the break statement. continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Continue-statement-python1

Syntax:



continue

Example:
Consider the situation when you need to write a program which prints the number from 1 to 10 and but not 6. It is specified that you have to do this using loop and only one loop is allowed to use.
Here comes the usage of continue statement. What we can do here is we can run a loop from 1 to 10 and every time we have to compare the value of iterator with 6. If it is equal to 6 we will use the continue statement to continue to next iteration without printing anything otherwise we will print the value.

Below is the implementation of the above idea:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to
# demonstrate continue
# statement
  
# loop from 1 to 10
for i in range(1, 11):
  
    # If i is equals to 6,  
    # continue to next iteration  
    # without printing 
    if i == 6:
        continue
    else:
        # otherwise print the value
        # of i
        print(i, end = " ")

chevron_right


Output:

1 2 3 4 5 7 8 9 10 

The continue statement can be used with any other loop also like while in a similar way as it is used with for loop above.

Exercise Problem:
Given a number n, print triangular pattern. We are allowed to use only one loop.

Input: 7
Output:
*
* * 
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

Solution: Print the pattern by using one loop | Set 2 (Using Continue Statement)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.