python break和continue

來源:互聯網
上載者:User

標籤:現在   代碼   tle   執行   ==   finish   rip   中斷   tin   

for m in range(3):    if m == 0:        for n in  range(3):            if n == 1:                print(m,n)                continue                print(n)            if n == 1:                print(m,n)
       if n == 2:
         print(m,n)
if m == 1: for x in range(3): if x == 1: print(m,x) break print(n) if x == 2: print(m,x)else: print(m,n,x)D:\untitled\venv\Scripts\python.exe D:/untitled/bogls/jia2.py0 1
0 21 11 2 1Process finished with exit code 0

認識python中的中斷,先來看兩句話,break:跳出整個for迴圈,continue:跳出本次迴圈(continue很少會用)。python代碼是從上往下執行的,迴圈會往複執行直到迴圈結束或者中斷。

然後分析上面的代碼

當’m==0‘時進入‘for n in range(3)’迴圈,當‘n==0’時迴圈裡的if語句中三個條件都不滿足,迴圈繼續進行‘n==1’滿足條件執行if語句輸出‘0,1’,然後執行continue跳出本次迴圈(沒有執行下一個輸出語句‘print(n)‘  同時跳出了‘n==1’的本次迴圈,不會判斷下面的兩個if語句),進入‘n==2’的迴圈,判斷三個if語句,第三個輸出(m,n)。

當‘m==1’時進入‘for x in range(3)’迴圈,當‘x==1’時滿足第一個if語句,輸出(m,n),然後執行break,跳出整個‘for x in range(3)’迴圈。現在只有一個總迴圈在執行‘for m in range(3)’,總迴圈結束執行else語句。

 

python break和continue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.