While-Loops
while-loop是依據條件來重複執行運算,如果while condition成立,執行statement
while conditions:
statement
範例
number=0
while number<50:
number = number * 10
print(number)
#output
10
20
30
40
50
break,continue
break,continue 這兩個指令可以控制迴圈
break:在碰到break之後運算不會執行,並且跳出迴圈
範例
number=0
while number<10:
number=number+1
if number>2:
break
print(number)
#output
3
continue:在碰到continue之後運算不會執行,並且回到迴圈開頭執行下一個迴圈
範例
number = 1
while number < 10:
number += 1
if number == 5:
continue
print(number)
#output
2
3
4
6
7
8
9
10