Python continue StatementPython continue keyword is used to skip the remaining statements of the current loop and go to the next iteration. In Python, loops repeat processes on their own in an efficient way. However, there might be occasions when we wish to leave the current loop entirely, skip iteration, or dismiss the condition controlling the loop. We use Loop control statements in such cases. The continue keyword is a loop control statement that allows us to change the loop's control. Both Python while and Python for loops can leverage the continue statements. Syntax: Python Continue Statements in for LoopPrinting numbers from 10 to 20 except 15 can be done using continue statement and for loop. The following code is an example of the above scenario: Code Output: 10 11 12 13 14 16 17 18 19 20 Explanation: We will execute a loop from 10 to 20 and test the condition that the iterator is equal to 15. If it equals 15, we'll employ the continue statement to skip to the following iteration displaying any output; otherwise, the loop will print the result. Python Continue Statements in while LoopCode Output: J v T p o i n t Explanation: We will take a string "Javatpoint" and print each letter of the string except "a". This time we will use Python while loop to do so. Until the value of the iterator is less than the string's length, the while loop will keep executing. Python Continue statement in list comprehensionLet's see the example for continue statement in list comprehension. Code Output: [4, 16, 36, 64, 100] Explanation: In the above code, list comprehension will square the numbers from the list. And continue statement will be encountered when odd numbers come and the loop will skip the execution and moves to the next iterartion. Python Continue vs. PassUsually, there is some confusion in the pass and continue keywords. So here are the differences between these two.
Next TopicPython Pass |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India