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

How to understand Python basic while loop and break, continue keywords

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to understand the Python basic while loop and break, continue keywords", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to understand the Python basic while loop and break, continue keywords"!

Catalogue

1. While loop syntax structure

2. + = and-= are used as exit conditions

2.1 + = used as an exit condition

2.2-= used as an exit condition

3. Break and continue keywords

3.1 use a case to illustrate the difference between the two

3.2 continue is used in combination with for loop and while loop

3.3 break is used in combination with for cycle and while cycle

Foreword:

There is no difference between for loop and while loop in Python, but in practical application, the pertinence is not the same.

For loop, mainly used in traversal, embodies the word traversal.

While loop, mainly used to judge the loop in accordance with the condition, reflecting the word condition.

1. While loop syntax structure

The while loop, which means that the program runs until the specified condition is no longer met. The syntax structure of the while loop is as follows:

While judgment condition (condition): execute code block (statements)...

When the judgment condition is True, the code block is executed; loop until the judgment condition is False, exit the loop. As you can see, the while loop must have an exit condition.

2. + = and-= are used as exit conditions

Looking at the syntax of the above while loop, the while keyword is followed by a "judgment condition", which is used to count. At first, the judgment condition has always been True. We need to use the + = and-= symbols to make the count increase or decrease until the judgment condition is False and exit the while loop.

If you have a good understanding of the concept, it may not be easy to understand. Let's go straight to the example.

Counts = 1while counts 0: print ("I'm so hungry!") Counts-= 1

The results are as follows:

I believe that after reading the example, it should be easy to understand, so I will not repeat it here, whether it is using + = forward counting or using-= reverse counting, it should be judged according to the actual situation.

3. Break and continue keywords

Sometimes, when the program loops to a certain condition, the subsequent loop does not need to be executed, and then execution is also resource-consuming, so the loop can be terminated, which can improve the execution efficiency of the program.

In Python, there are two keywords continue and break for us to use. Remember one thing: the keywords continue and break can be used in combination with the for loop and the while loop.

3.1 use a case to illustrate the difference between the two

Imagine an interviewer interviewing ten people, one at a time.

When interviewing someone, someone suddenly called the interviewer and said, his house is on fire, which is equivalent to break, and then the interview does not have to take place again, equivalent to the end of the cycle.

When interviewing someone, someone asks you how much 1 + 1 equals, and you say no, so the interviewer says, the next person, which is equivalent to continue, just ends the current cycle, and the rest of the cycle has to be executed one by one.

3.2 continue is used in combination with for loop and while loop

We already know that countinue is used to end the current loop, and the block of code after the current loop will not be executed. But the whole cycle will continue.

① continue + for cycle

For i in range (5): if I = = 3: print ("code before continue will execute") continue print ("code after continue will not execute")

The results are as follows:

② continue + while cycle

I = 5while I > 0: I-= 1 if I > = 3: print ("I will execute before continue") continue print ("I won't execute after continue")

The results are as follows:

3.3 break is used in combination with for cycle and while cycle

Break is more ruthless, directly terminating the cycle.

① break + for cycle

For i in range (5): print (f "I = {I}") if I > = 3: break

The results are as follows:

② break + while cycle

For the while loop, I wrote two pieces of code here.

I = 0while I = 3: break---i = 0while I = 3: break I + = 1

The results are as follows:

At this point, I believe you have a deeper understanding of "how to understand the Python basic while loop and break, continue keywords". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report