Python control flow continue statement

little bottle
Release: 2019-04-08 11:53:00
forward
2306 people have browsed it

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;)
    # 自此处起继续进行其它任何处理
Copy after login

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
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!