Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the break statements and continue statements in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the break sentence and continue sentence in python, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Getting started with python-08 break statement and continue statement

Example of break statement

Enter:

#! / usr/bin/python

# Filename: break.py

While True:

S = (input ('Enter something:'))

If s = = 'quit':

Break

Print ('Length of the string is', len (s))

Print ('Done')

Output:

Enter something: Programming is fun

Length of the string is 18

Enter something: When the work is done

Length of the string is 21

Enter something: if you wanna make your work also fun:

Length of the string is 37

Enter something: use Python!

Length of the string is 12

Enter something: quit

Done

Explanation:

In the loop statement, break appears, indicating that the loop is interrupted at this time.

In this example, the while loop is used, and the condition after while is TURE, indicating that the loop will continue to repeat.

In the loop statement, an if conditional statement is nested, and when the variable is assigned to "quit", the break operation is performed.

The first four inputs are numeric, so the loop continues, while the fifth input matches "quit", the loop ends, and all the code of the output Done is executed.

Continue statement

Enter:

#! / usr/bin/python

# Filename: continue.py

While True:

S = input ('Enter something:')

If s = = 'quit':

Break

If len (s) < 3:

Print ('Too small')

Continue

Print ('Input is of sufficient length')

# Do other kinds of processing here...

Output:

Enter something: a

Too small

Enter something: 12

Too small

Enter something: abc

Input is of sufficient length

Enter something: quit

Explanation:

In the loop statement, continue appears, which means that the single loop is interrupted at this time and execution starts again.

In this example, in the while loop, the length of the input is determined by adding the if condition. Contine is executed when the length of the input is less than 3, and subsequent print output statements are never executed. When the continue statement is not executed when the input length is greater than or equal to, the print output statement after it has a chance to execute.

Summary:

Break is used to terminate the loop statement

When using continue, in order to quickly stop the current cycle, restart the execution of a new cycle.

After reading the above, do you have any further understanding of the break statement and continue statement in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report