1.if...elif...else ...
1Number = 232guess = Int (raw_input ('Enter an integer:'))3 4 ifGuess = =Number :5 Print('Congratulations, you guessed it.')#New block starts here6 Print("(but don't win any prizes!)")#New block ends here7 elifGuess <Number :8 Print('No, it's a little higher than that')#another block9 Else:Ten Print('No, it's a little lower than that' ) One A Print(' Done' ) - #This last statement are always executed, after the IF statement is executed
2.while
1Number = 232running =True3 4 whileRunning:5guess = Int (raw_input ('Enter an integer:'))6 7 ifGuess = =Number :8 Print 'Congratulations, you guessed it.'9running = False#This causes the and loop to stopTen elifGuess <Number : One Print 'No, it's a little higher than that' A Else: - Print 'No, it's a little lower than that' - Else: the Print 'The while loop was over .' - #Do anything else want - - Print ' Done'
When the loop condition becomes false, the else block is executed.
You might want to ask if such an else block will always be executed and what else will be done. Don't worry, there's a solution in 4.break.
3.for
1 for in range (1, 5):2 print i3else: 4 print'the ForLoopwas over'
Range (n, m [, steplen]) = [n,n+steplen,n+steplen*2,..., n+ (Ceil ((m-n)/steplen)-1) *steplen]
That is, starting with N, the Steplen is the step length, which is a sequence of less than m (not including m). By default, Steplen=1.
4.break
Terminates the current loop statement.
Note If you terminate from a for or while loop, any corresponding else will not be executed.
5.continue
Skips the remaining statements in the current loop block, and then proceeds to the next round of loops.
Control flow of the Python language (if...elif...else,while,for,break,continue)