程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Usage of break statement and continue statement in Python

編輯:Python

stay Python in ,break Statement and continue Statements are generally used in loop statements .

1 break sentence

1.1 break Statement in a single loop

In a single loop break sentence , Its function is to end the current cycle , Code as shown 1 Shown .

chart 1 break End the current cycle

From the picture 1 It can be seen that , stay for In circulation , When i The value of is 3 when , Would call break Statement ends the current loop , The output at this time is 0-3, in other words , When i be equal to 3 when , The loop is break The sentence is over .

1.2 break The use of statements in multiple loops

Multiple loops refer to the nesting of loops , That is, the loop statement contains other loop statements , Pictured 2 Shown .

chart 2 Double loop code

From the picture 2 It can be seen that , The code is the first for The loop contains a loop statement , The input at this time is shown in the figure 2 Medium blue part .

Used in multiple loops break Statement can only end the loop associated with it , Instead of ending all the cycles , Pictured 3 Shown .

chart 3 Use... In double loop code break

From the picture 3 It can be seen that , In the figure 3③ In position break Statement can only end graph 3② In position for loop , Instead of ending the diagram 3① In position for loop , The output at this time is shown in Figure 3④ Shown .

2 continue sentence

continue Statement ends this loop , And return to the beginning of the loop statement , Judge whether to continue executing the loop statement according to the condition , It is not like break Statement that is to end the entire loop statement .

2.1 Use... In a single cycle continue sentence

Code as shown 4 Shown .

chart 4 Use... In a single cycle continue sentence

  From the picture 4 It can be seen that , stay for In circulation , When i The value of is 2 when , call continue Statement ends this loop , It will not be executed at this time print() Statement and back for Execute the next loop at the beginning of the statement . therefore , There is no... In the output at this time 2.

2.2 Use in multiple loops continue sentence

And “1.2 break The use of statements in multiple loops ” Mentioned in break Sentence similarity ,continue A statement can only end the one associated with it for Statement , The code will not repeat .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved