1.for迴圈 2.while迴圈 3.break 4.continue ...
1.for迴圈
a =0 for b in range(101): a =a +b print a
>>> list (range(5))
[0, 1, 2, 3, 4]
2.while迴圈
>>> sum = 0 >>> n= 99 >>> while n>0: ... sum =sum + n ... n = n-2 ... >>> print sum 2500
3.break
n = 1 while n <= 100: if n > 10: # 當n = 11時,條件滿足,執行break語句 break # break語句會結束當前迴圈 print(n) n = n + 1 print('END')
4.continue
n = 0 while n < 10: n = n + 1 if n % 2 == 0: # 如果n是偶數,執行continue語句 continue # continue語句會直接繼續下一輪迴圈,後續的print()語句不會執行 print(n)