This is the sixth day of my participation in the August Text Challenge.More challenges in August @[TOC]

preface

What if I need to set up an infinite loop and terminate it with a condition?

A very simple problem, but I did not say, this article should be in the branch control statement, now can only take out hydrology to write another

The addition of conditional control statements

Instead of stopping the entire program, we can manually terminate the loop with a keyword.

Three statements can stop the loop: break return continue

A return is usually a function that uses a break

Continue is not used to terminate a loop, as shown in the following code example

break

Look at the Chinese also have a general idea: break

Its function is to break the cycle so that the cycle stops when it hits it

Break breaks the structure, causing the program to exit the current code block

In this example, what we want to do is loop through the data that the user enters from the keyboard until q is entered to launch the program

while True:
    a = input(a)if a == "q":
        break
    else:
        print(a + "Entered successfully, enter 'q' to exit the program.")
Copy the code

Running results:

At this point, we find that the input Q comes out, but we are not comfortable with it, we can also add a hint, or ask him to enter “confirm” again to confirm whether to come out

while True:
    a = input(a)if a == "q":
        print("Sure to push exit, if yes, please enter OK exit to confirm.")
        if input() = ="Definitely out":
            print("Exit, program terminated.")
        break
    else:
        print(a + "Entered successfully, enter 'q' to exit the program.")
        
Copy the code

Running results:

This is perfect, I this is not who words ah, I this is obsessive-compulsive (serious face)!

continue

“Continue” is to break out of the current loop and execute the next loop, that is, it does not have the ability to terminate the loop, it can only make the loop loop fewer times

Let’s try changing break to continue in the code above

while True:
    a = input(a)if a == "q":
        print("Sure to push exit, if yes, please enter OK exit to confirm.")
        if input() = ="Definitely out":
            print("Exit, program terminated.")
        
    else:
        print(a + "Entered successfully, enter 'q' to exit the program.")
        
Copy the code

It won’t work

Can’t quit!

Sure enough, what does continue do?

I don’t say, you see:

for i in range(10) :if i % 5= =0:
        continue
    else:
        print(i)  
Copy the code

Running results:

return

Return means to return, and is used to return a value in a function, which we’ll cover in more detail in this column

We calculate that when I goes up to 5, we let a+b, and then we terminate

When I is 5, the loop is forcibly terminated regardless of whether it has finished

def sum(a, b) :
    for i in range(10) :if i<a:
            pass
        else:
            a+=b
            return a
print(sum(5.2))
Copy the code

Pass means do nothing, right

The result is 7

So what if we change the return position?

def sum(a, b) :
    for i in range(10) :if i<a:
            pass
        else:
            a+=b
        return a
print(sum(5.2))
Copy the code

Our loop is going to terminate the first time, because the first time I is zero, we’re going to execute a return statement, we’re going to terminate the loop

So what if we switch again? I’m going to put it outside of the loop, so I’m going to finish the loop, and I’m going to do a+=b once, again 7

def sum(a, b) :
    for i in range(10) :if i<a:
            pass
        else:
            a+=b
    return a
print(sum(5.2))
Copy the code

conclusion

I was going to write iterator generators as well, but I decided not to

If you can understand this, then it’s not going to be difficult, and I don’t want to write very simple examples, and it’s not that hard, but it’s a little bit convoluted and a waste of resources.

I hope that not only have you learned break, continue, and return in this blog post, but you’ve also learned about indentation, code hierarchy, and how code works. That would be great

Interest is the best teacher, persistence is the invariable truth. Learn not to be impatient, one step at a time, step forward steadily. Make a little progress every day, and over a long period of time, you will find that you have become very strong.

I am Bu Xiaochan, a cute new self-study, follow me every day a little bit of progress!

This is enough for the time being, so be on your way