grammar
while 条件:
执行代码...
It is simple, while
that is, when the meaning of the mountain when no edges and corners, and when the river. . . , Sorry, while
as it comes back condition is satisfied, executes while
the following code
Let write a program to print programs from 0 to 100, each cycle, + 1
count = 0
while count <= 100 : #只要count<=100就不断执行下面的代码
print("loop ", count )
count +=1 #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0
Export
loop 0
loop 1
loop 2
loop 3
....
loop 98
loop 99
loop 100
If I want to print even realize 1-100 how to do it?
They would have to figure out, how to determine whether a number is even, divisible by 2 is an even number, right, how to determine whether divisible by 2? Simple, in addition to not complete the remainder is two friends. Remember we learned modulo operator count it?
10% 2 >>>
0
>>> # 2 8% no remainder, is even 0
>>> 7% more than the number # 2, odd
1
Put in our recycling program
= COUNT 0
the while count <= 100: # long count <= 100 to continuously execute the following code
if count% 2 == 0: # is even
Print ( "Loop" , COUNT)
COUNT =. 1 + # Each time, it the count + 1, or else it becomes an infinite loop, because the count has been 0
Export
loop 0
loop 2
loop 4
loop 6
....
loop 96
loop 98
loop 100
Endless loop
There is a cycle called an infinite loop, once triggered a run on forever, the highest power.
while behind as long as conditions are met (that is, the condition is true) has been executed, how the conditions have been set up so that it?
= COUNT 0
the while True: #True itself is really ah
print ( "You are the wind I am the sand, smell the End of the World ..." , COUNT)
COUNT = 1 +
The cycle is aborted statement
If in the course of the cycle, for some reason, you do not want to continue the cycle, and how it suspend off it? This use break or continue statement
- end of a break for complete cycle, back out of the loop execution loop
- continue and break somewhat similar, except that only continue to terminate the present cycle, and then later further execution cycle, break the loop completely terminated
Examples: break
= COUNT 0
the while count <= 100: # long count <= 100 to continue execution following code
Print ( "Loop" , COUNT)
IF COUNT ==. 5 :
BREAK COUNT +. 1 = # Each time, put the count + 1 , or else it becomes an infinite loop, because the count has been 0 Print ( "----- ------ OUT of the while loop")
Export
loop 0
loop 1
loop 2
loop 3
loop 4
loop 5
-----out of while loop ------
Examples: continue
= count 0
the while count <= 100 :
count + =. 1
IF count> count. 5 and <95: # long count between 6-94, the following print statement not go directly into the next Loop
Continue
print ( "Loop " , COUNT)
Print (" ----- ------ OUT of the while Loop ")
Export
loop 1
loop 2
loop 3
loop 4
loop 5
loop 95
loop 96
loop 97
loop 98
loop 99
loop 100
loop 101
-----out of while loop ------
while … else ..
And generally only if used with different other languages else, in Python there is a while ... else statement
else while the latter refers to the effect, when the while loop normally been executed, are suspended with no break, then it will perform the back else
= COUNT 0
the while COUNT <=. 5 :
COUNT = +. 1
Print ( "Loop" , COUNT)
the else : Print ( "done for normal execution loop" ) Print ( "OUT of the while Loop ----- ----- - ")
Export
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环正常执行完啦
-----out of while loop ------
If the implementation process is break it, it will not execute the else statement it
count = 0
while count <= 5 :
count += 1
if count == 3:break
print("Loop",count)
else:
print("循环正常执行完啦")
print("-----out of while loop ------")
Export
Loop 1
Loop 2
-----out of while loop ------