for - continue

  1. for 迴圈在某條件發生時,不執行後面的程式 (statement3),可以使用 coutinue 繼續迴圈,語法如下:
    for iterating_var in sequence:
    	statement1(s)
    	if expression:
    		statement2(s)
    		coutinue
    	statement3(s)
    
  2. 範例:輸入一分數,統計分數串列中大於此分數的個數。
    [dywang@deyu zzz]$ cat loop7.py 
    #!/usr/bin/env python3
    # coding: utf-8
    
    num = int(input("Enter a number: "))
    scores = [ 15, 93, 47, 62, 45, 88, 20, 79, 31, 65, 18, 27 ]
    count = 0
    for i in scores:
    	if i < num: continue
    	count += 1
    print("total=%d" % count)
    
  3. 執行結果,大於 60 分有五位,大於 80 分有二位。
    [dywang@deyu zzz]$ ./loop7.py 
    Enter a number: 60
    total=5
    [dywang@deyu zzz]$ ./loop7.py 
    Enter a number: 80
    total=2