continue Statement in Python

Spread the love
Contents:
  • continue Statement
  • Example
continue Statement

In Python, continue Statement  is a control flow statement that is used within while loop or for loop, skips the current iteration of the loop and bring control to the top of the loop to continue next iteration of the loop. In simple word, if a continue statement is encountered within loop, all the statements written below continue statement will be ignored and control goes to top of the loop to continue the next iteration of the loop.

Example

# Write a program in Python that prints all the odd numbers starting from 1 to 10.

x = 1
while(x <= 10):
          if x%2 == 0:
                  x = x + 1
                  continue   #  iteration stops and control goes to the top of the loop for next iteration, ignoring rest of the statements 
         print(x, end = ‘ ‘)
         x = x + 1

Output:
1 3 5 7 9
Explanation: Here, when continue statement is encountered after numbers being verified as even numbers in if test expression, the statements written after continue are ignored and control goes to top of the loop for the next iteration and prints all the odd numbers.  

🙂 Happy Learning !

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top