iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 14
0
自我挑戰組

Python初學者的自學筆記系列 第 14

Day14 pass、break、continue

Pass:pass這個述句不會做任何的事情,但它能讓你在不知道要放什麼述句時,一個讓你的程式符合語法的述句。
例句

count=0
for string in 'test':
    count+=1
    if string == 's':
     pass
print('\n迴圈結束')
print('迴圈執行了 %d 次' %count)

結果:
迴圈結束
迴圈執行了 4 次
Break:在使用while或者是for迴圈時,如果已經達成條件了,就強制跳出整個迴圈,這樣就不用繼續執行下去了。
例句

count=0
for string in 'test':
    count+=1
    if string == 's':
        break
    print(string)
print('\n迴圈結束')
print('迴圈執行了 %d 次' %count)

結果:
t
e
迴圈結束
迴圈執行了 3 次

Continue:continue這個述句是在你達成條件時,就跳出這一次的迴圈,但是仍然會繼續執行迴圈

count=0
for string in 'test':
    count+=1
    if string == 't':
        continue
    print(string)
    
print('\n迴圈結束')
print('迴圈執行了 %d 次' %count)

結果:
e
s

迴圈結束
迴圈執行了 4 次


上一篇
Day13迴圈
下一篇
Day15集合(一)
系列文
Python初學者的自學筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言