The previous section simply tells the Python judgment statement, and the loop statement, if, while, for, and so on
In this section, we'll take a closer look at some of the statements within the loop
Break
Break in Python is the same as other languages, and you can jump out of a looping statement
In general, some loop statements can have else, and if a loop is terminated by a break, then the statement under else will be executed
For example, by nesting A for loop for all prime numbers in a range (prime is also called prime number, greater than 1, except 1 and self, numbers that cannot be divisible by other numbers are called prime numbers)
In [2]: for n in range (2): ...: for m in range (2, N): ...: if n% m = = 0: ...: print (n, "not a quality ") ... : Break ...: Else: ...: print (N," is a prime number ") ... : 2 is a prime number 3 is a prime number 4 is not a prime number 5 is a prime number 6 Not a prime number 7 is a prime number 8 is not a prime number 9 is not a prime number
Note else is the same sibling as the second for loop
I simply say the above code, the first is an outer for loop, and then there is a for loop and an else statement, because there is no judgment statement, so the following else statement, is sure to execute, but wait until the previous for loop execution, will not execute, And there is a break statement in the for loop that jumps out of the entire for loop, and does not execute the following else statement, so that if the integer divisible is not prime numbers are skipped directly to else, so this completes a prime function
Continue
It is the same as the continue of other languages, or it is directly referring to the C language
Jumping out of the current loop, notice that break is jumping out of the loop, and continue just jumps out of the current loop, continue the statement behind the
For example, if you add continue to the last line, then in fact he hasn't changed anything.
Below we demonstrate the continue statement with an even number of odd numbers.
In [2]: to num in range (2, ten): ...: if num% 2 = = 0: ...: print (num, "is an even number") ... : continue
...: print (num, "is an odd number") ... : 2 is an even 3 is an odd number 4 is an even 5 is an odd number 6 is an even 7 is an odd 8 is an even 9 is an odd number
Pass
It can be said that Python alone
Pass can be used as a stand-alone statement, when the fact that he does nothing, for example, when doing a project, you just think of a functional method, but you have not yet to implement it, you can add a pass under the definition of the method, and then you can temporarily ignore this method, and go in the document to write other functions , so it can guarantee that your program doesn't get an error.
For example, I write one of these two pieces of code
In [3]: While True: ...: Pass
And then it runs, then this becomes a dead loop, and you can only force the program to stop by other means.
Pass is most commonly used when defining a class or a method (of course we haven't talked about methods and classes yet)
Like what
In [4]: Def myemptyfunction (): ...: Pass
In [6]: Class Myenptyclass: ...: Pass
About judging the loop statement is basically these
08_python control Judgment Loop Statement 2 (break, continue) _python programming Road