Python continue StatementIn this tutorial, we'll look at how to use Python continue keyword to skip the remaining statements of the current loop and go to the next iteration. Also, the difference between continue and pass keywords. Application of the Continue StatementIn 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. The continue KeywordIn Python, the continue keyword return control of the iteration to the beginning of the Python for loop or Python while loop. All remaining lines in the prevailing iteration of the loop are skipped by the continue keyword, which returns execution to the beginning of the next iteration of the loop. Both Python while and Python for loops can leverage the continue statements. Example of Python Continue Statements in For LoopAssume the following scenario: we want to develop a program that returns numbers from 10 to 20 but not 15. It is mentioned that we must perform this with a 'for' loop. Here's when the continue keyword comes into play. 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. The following code is an example of the above scenario: Code Output: 10 11 12 13 14 16 17 18 19 20 Now will repeat the above code, but this time with a string. 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. Code Output: J v T p o i n t 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
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week