Python solid roll base (b) else interesting

We often use branches to do else if statement in judge today found that loop for, while statements can also be used else do branch.

. 1  for I in Range (. 3 ):
 2      Print (I)
 . 3  else :
 . 4      Print ( " ! Good " )
 . 5  
. 6  # output can be seen after cycle through else statement is executed 
. 7  0
 . 8 . 1
 . 9 2
 10  Good!
. 11  
12 is  # increase BREAK 
13 is  for I in Range (. 3 ):
 14      Print (I)
 15      BREAK 
16  the else :
 . 17      Print (" Good! " )
 18  # outputs can be found traversing the first increase in break exits the loop without executing the else statement 
19 0

to sum up:

for, after using a while loop break break statement, else statement is executed, the successful implementation of the completion of the loop without executing the else statement.

Under certain scenarios, reducing the need for setting flag marker variables, save the code work.

 

Guess you like

Origin www.cnblogs.com/yydada/p/11671045.html