# @Time
# @File : While loop. py
Example 1:
1 n = 02 while True:3 print'Hello World'4 if n = = Ten:5break 6 N +=1
Print as follows:
1 Hello World 2 Hello World 3 Hello World 4 Hello World 5 Hello World 6 Hello World 7 Hello World 8 Hello World 9 Hello World Ten Hello World Hello World
Example 2:
1 n = 02 while True:3 if n==10:4 break 5 print N,'hello'6 N +=1
Print as follows:
1 0 Hello 2 1 Hello 3 2 Hello 4 3 Hello 5 4 Hello 6 5 Hello 7 6 Hello 8 7 Hello 9 8 Hello
10 9 Hello
Example 3:
#条件为假的时候, also exits the loop. Exit the loop when you enter a null character
1Rinput ="'2 whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4 Print 'Hello'5 ifRinput = ="':6 Break
Example 4:
#应用逻辑非判断, exit the while loop. While condition is false, exits while loop
1Rinput ="'2 whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4 Print 'Hello'5 if notRinput:6 Break
Example 5:
# While...else ...: The statement that follows else is printed when the normal end while loop. With For...else ... The same as the loop body
1Rinput ="'2 whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4 Print 'Hello'5 if notRinput:6 Break7 Else:8 Print 'Hello World'
Example 6:
#while loop nested If loop body, support contine
1Rinput ="'2 whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4 Print 'Hello'5 if notRinput:6 Break7 elifRinput = ='quite':8 Continue9 Print 'Continue'Ten Else: One Print 'Hello World'
Python Foundation 2.4 While Loop