The next step is to understand the loop statement. As we all know, programs are normally executed in order. Programming languages provide a variety of control structures that allow for more complex execution paths. Looping statements allow us to execute a statement or group of statements multiple times, following the general form of a looping statement in most programming languages:
Python provides a For loop and a while loop (there is no do in Python: While loop), let's look through the form:
Loop Type |
Description |
While loop |
Executes the loop body when the given judgment condition is true, otherwise exits the loop body. |
For loop |
Repeating statements |
Nested loops |
You can nest A for loop in the while loop body |
And then, as we know, loop control statements can change the order in which statements are executed, so let's look at the loop control statements that Python supports:
Control Statements |
Description |
Break statement |
Terminates the loop during statement block execution and jumps out of the loop |
Continue statements |
Terminates the current loop during statement block execution, jumps out of the loop, and executes the next loop. |
Pass Statement |
Pass is an empty statement in order to maintain the integrity of the program structure. |
Let's first look at the while Loop statement. In Python programming, a while statement is used to loop the execution of a program, which, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:
While judging condition: Execute statement ...
The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true. The loop ends when the condition false false is judged. Let's take a look at the execution process diagram:
Let's take a look at another, GIF demo of Python's While execution process diagram:
may not be able to demonstrate, hehe, that even, let's take a direct look at the example:
#!/usr/bin/pythonCount=0while ( Count9 : print ' the count Is: count count = count + 1 print "Good bye!
The above code executes the output result:
TheCountIs: 0TheCountIs: 1TheCountIs: 2TheCountIs: 3TheCountis: 4the count is: 5the count is : 6the count is: 7 the count is: 8good bye !
While statement there are two other important command Continue,break to skip the loop, continue used to skip the cycle, break is used to exit the loop, here, the introduction may not be comprehensive, but in the following article will focus on the two commands. In addition, the "judging condition" can also be a constant value, indicating that the loop must be set up, the following is the specific usage:
# Continue and break usage I=1While I<10:I+=1 If I%2>0:# Skip output when non-even Continue Print I # outputs 2, 4, 6, 8, 10 I=1while 1: # cycle condition of 1 must be set up print< Span class= "Hl-code" > i # output 1~10 i + = 1 if i > 10 # when I is greater than 10 o'clock jump out of the loop break
We need to know that if the conditional judgment statement is always true, the loop will execute indefinitely:
#!/usr/bin/python#-*-Coding:utf-8-*- Var=1While Var = = 1: # the condition is always true, The loop will execute indefinitely num = Span class= "Hl-builtin" >raw_input ( " Enter a number: ") print you entered: " num< Span class= "Hl-code" > print "good bye!
The result of the above example output:
EnterA number:20YouEntered: 20EnterA number:29YouEntered: 29EnterA number:3YouEntered: 3enter A number between :traceback (most recent call last): file "test.py" , line 5, in<module> num = Raw_input ( "Enter a number:" ) Keyboardinterrupt
Note: above the infinite loop you can use CTRL + C to interrupt the loop.
So, in Python, while ... else executes the ELSE statement block when the loop condition is false:
#!/usr/bin/python Count=0While Count < 5: print count " is less than 5" count = count< Span class= "Hl-code" > + 1else: print count " is not Less than 5 "
The result of the above example output is:
0 is less than 51 is less than 5 2 is less than 53 is less than 54 is less than 55 is not less than 5
Now let's take a look at the simple statement group. Similar to the IF statement syntax, if you have only one statement in the while loop body, you can write the statement in the same row as the while, as follows:
#!/usr/bin/ Python flag = 1< Span class= "Hl-code" > while (flag) : Print ' given flag is really true!" print good bye!
Note: above the infinite loop you can use Ctrl + C to interrupt the loop.
OK, about while sharing to here is almost over, thank you appreciate, if you feel good, please more praise support oh ...
python2.7 Getting Started---looping statements (while)