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.
The execution flowchart is as follows:
GIF demo Python while statement execution procedure
Instance#!/usr/bin/pythonCount=0 while ( count < 9) : print ' the count Is: ' , < Span class= "Hl-identifier" >count count = count + 1 print < Span class= "hl-quotes" > "good bye!"
Run an instance?
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, in addition, "judging condition" can also be a constant value, indicating that the loop must be set up, the following:
# Continue and break usageI=1WhileI<10:I+=1IfI%2>0:# Skip output when non-evenContinuePrintI# outputs 2, 4, 6, 8, 10I=1 while 1: # loop condition of 1 must be established print i # output 1~10 i += 1 if < Span class= "Hl-identifier" >i > 10 : # when I is greater than 10 o'clock jump out of the loop break
Infinite loops
If the conditional judgment statement is always true, the loop will execute indefinitely, as in the following example:
Instance#!/usr/bin/python#-*-Coding:utf-8-*-Var=1WhileVar==1 : # The condition is always true, and the loop will execute indefinitely with num = raw_input("Enter a number:") . C13>print "Youentered: ", num 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.
Looping with Else statements
In Python, while ... else executes an ELSE statement block when the loop condition is false:
Instance#!/usr/bin/pythonCount=0WhileCount < 5: print count , " is less than 5" count = < Span class= "Hl-identifier" >count + 1 else: 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
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:
Instance#!/usr/bin/python Flag = 1 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.
Python while loop statement