Home  >  Article  >  Backend Development  >  Python control flow continue statement

Python control flow continue statement

little bottle
little bottleforward
2019-04-08 11:53:002391browse

This article mainly talks about the continue statement in Python control flow. The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue with the next iteration of the loop.

Case (save as <span style="font-family: 宋体, SimSun;">continue.py</span>):

while True:
    s = input(&#39;Enter something : &#39;)
    if s == &#39;quit&#39;:
        break
    if len(s) < 3:
        print(&#39;Too small&#39;)
        continue
    print(&#39;Input is of sufficient length&#39;)
    # 自此处起继续进行其它任何处理

Output:

$ python continue.py
Enter something : a
Too small
Enter something : 12
Too small
Enter something : abc
Input is of sufficient length
Enter something : quit

How it works

In this program , we accept input from the user, but we will only process the input string if it is at least 3 characters long. To do this, we use the built-in len function and get the length of the string, if its length is less than 3, we skip the rest of the statements in the code block by using the continue statement. Otherwise, the remaining statements in the loop will be executed and any type of processing we wish will take place here.

It should be noted that the continue statement can also be used for for loops.

Summary

We have learned about three types of control flow statements - if, while and for - and how its related break and continue statements work. These statements are some of the most commonly used parts of Python tutorials, so getting used to using them is necessary.

[Recommended course: Python video tutorial]

The above is the detailed content of Python control flow continue statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete