Acquaintance python - Process Control While

grammar

  1. while 条件:
  2. 执行代码...

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 whilethe following code

Let write a program to print programs from 0 to 100, each cycle, + 1

  1. count = 0
  2. while count <= 100 : #只要count<=100就不断执行下面的代码
  3. print("loop ", count )
  4. count +=1 #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0

Export

  1. loop 0
  2. loop 1
  3. loop 2
  4. loop 3
  5. ....
  6. loop 98
  7. loop 99
  8. 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

  1. loop 0
  2. loop 2
  3. loop 4
  4. loop 6
  5. ....
  6. loop 96
  7. loop 98
  8. 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

  1. loop 0
  2. loop 1
  3. loop 2
  4. loop 3
  5. loop 4
  6. loop 5
  7. -----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

  1. loop 1
  2. loop 2
  3. loop 3
  4. loop 4
  5. loop 5
  6. loop 95
  7. loop 96
  8. loop 97
  9. loop 98
  10. loop 99
  11. loop 100
  12. loop 101
  13. -----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

  1. Loop 1
  2. Loop 2
  3. Loop 3
  4. Loop 4
  5. Loop 5
  6. Loop 6
  7. 循环正常执行完啦
  8. -----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

  1. Loop 1
  2. Loop 2
  3. -----out of while loop ------

Guess you like

Origin www.cnblogs.com/jhui104/p/11444103.html