Python While loop statement

In Python programming, a while statement is used to loop through a program, that is, under certain conditions, a loop of a program is executed to handle the same task that needs to be repeated. The basic form is:

while Conditions: < /span>
     Execute statement...

The execution statement can be a single statement or a block of statements. The judgment condition can be any expression, and any non-zero, or non-null (null) value is true.

When the condition is false, the loop ends.

The flow chart is as follows:


Gif demonstrates Python while statement execution process


#!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"

The above code executes the output result:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

The while statement has two other important commands continue, break to skip the loop, continue to skip the loop, break is used to exit the loop, and the "judgment condition" can also be a constant value, indicating that the loop must be Established, the specific usage is as follows:

# continue with break usage i = 1 while i < 10: i += 1 if i%2 > 0: # Skip output when non-double continue print i # Output double number 2、4、6、8、10 i = 1 while 1: # The loop condition is 1 must be established print i # Output 1~10 i += 1 if i > 10: # Jump out of the loop when i is greater than 10 break

Infinite loop

If the conditional statement is always true, the loop will execute indefinitely, as in the following example:


#!/usr/bin/python # -*- coding: UTF-8 -*- var= 1 while var == 1 : # The condition is always true and the loop will execute indefinitely num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!"

The above example output:

Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
File "", line 5, in <module>
num = raw_input("Enter a number :")

Note: For an infinite loop above, you can use CTRL+C to break the loop.

Recycling the else statement

In python, while ... else executes the else statement block when the loop condition is false :


#!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5"

The above example output is:

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

Simple statement group

Similar to the if statement syntax, if you have only one statement in your while loop body, you can write the statement in the same line as while. As shown below:


#!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!"

Note: For an infinite loop above, you can use CTRL+C to break the loop.

welookups is optimized for learning.© welookups .
All Right Reserved and you agree to have read and accepted our term and condition