stay Python in ,break Statement and continue Statements are generally used in loop statements .
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 .
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 .
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 .
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.
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 .