Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

We’ve seen Python for, if, and assignment statements before. In this article, we’ll look at two control flows in Python: break and continue

First of all, from the English word literally, we can simply understand the two:

  • The meaning of break in the code is to interrupt the operation of the following code
  • Continue means to continue the execution of the following code

Second, we need to understand that both have the function of breaking out of loops. Both can be used in for loops and while loops, but the scope of action is different.

This article mainly explains two main knowledge points:

  • Break and continue statements
  • The impact of indentation on a code block

Range function

The range(m,n,s) functions in the following examples take the values of the specified range of values as iterators:

  • M: starting position, integer; The default value starts at 0 and can be m; Can be omitted
  • N: terminating position, integer, must have, cannot get n; Don’t omit
  • S: step length, the length of each step; Can be positive or negative

Here are some examples:

Single-layer for loop -break

Example 1:

for i in range(8) :# I = 0,... 7
    if i == 5:  Break (I = 5
        break
    print(i)
    
# the results
0
1
2
3
4
Copy the code

What explains the above results?

  • When I =0, enter if, not equal to 5, do not execute break, print directly, then output 0
  • Similarly, I =1,2,3,4 is the same operation
  • When I =5 enters the if statement, execute break, break the loop, and the following I =6 and 7 will not execute

Look at example 2 (only without indentation) :

for i in range(8) :# I = 0,... 7
    if i == 5:
        break
print(i)

# the results
5
Copy the code

This example illustrates the impact of indentation on code execution:

  • If I =0, not equal to 5, do not execute break, enter the next loop that is I =1
  • Similarly, if I =1, 2, 3, 4 is not equal to 5, and break is not executed
  • When I =5 enters if, it happens to be equal to 5. Execute the break statement to break out of the loop, and I happens to be 5. Print (I) 5

Example 3:

The result of this example is interpreted as in example 1, except that the range iterator is a string:

for i in "python":
    if i == "o":
        break
    print(i)
    
# the results
p
y
t
h
Copy the code

The three break-related examples show that when a break condition is met, the entire for loop is broken; Of course, this is just for single-layer loops.

Single-layer for loop -continue

Example 4:

for i in range(8) :# I = 0,... 7
    if i == 5:
        continue
    print(i)
    
# the results
0
1
2
3
4
6
7
Copy the code

Explain the results above:

  • When I =0 goes into the if loop, not equal to 5, do not continue, print(I), print 0; Enter the next cycle
  • When I =1, 2, 3, and 4 enter the if loop, the effect is the same and print(I) is executed
  • When I =5 enters the if loop and I is equal to 5, continue print(I); Enter the next loop directly from the beginning
  • When I =6, 7 is the same as when I =0, 1, 2, 3, 4

What if the print statement does not indent? Look at example 5:

for i in range(8) :if i == 5:
        continue
print(i)

# the results
7
Copy the code

You can see from example 5 above that the last time I is equal to 7; When our print statement does not indent, it will only execute when the for statement is fully executed, so it won’t print until the last I =7.

Example 6:

for i in ["Chinese"."Mathematics"."English"] :ifi ! ="Mathematics":
        continue
    print(i)
    
# the resultsmathematicsCopy the code

Explain the results above:

  • “I” = “language”; “I” = “math”; “continue” = “print”; Enter the next cycle
  • The second time I is “math”, it goes into if, it doesn’t satisfy the if condition, so instead of continue, print “math”
  • The following “English” satisfies the if condition, and continues directly

The three continue examples above show that when conditions for a continue statement are met, the current statement is not executed, but subsequent statements are not executed

Gauss sum

Example 7:

sum = 0  # the initial value of the sum

for i in range(1.101) :if i % 2= =0:  The remainder of # I divided by 2 is 0, which means I is even
        break
    sum += i  # sum = sum + i
print(sum)
   
# the results
1
Copy the code

Explain the results above:

  • When I =1 enters if, sum += I is executed, and sum becomes 1 and I becomes 2 to enter the next loop
  • When I =2 enters if and the even condition is met, break the loop. Sum + I is not executed again
  • Print (sum) is actually the sum value of step 1

Example 9:

sum = 0

for i in range(1.101) :# I goes from 1 to 100
    if i % 2= =0:    The remainder of # I divided by 2 is 0, which means I is even
        continue
    sum += i
print(sum)
        
# the results
2500
Copy the code

Explain the results above:

  • When I =1 enters the if loop and the if condition is not met (I is odd), executesum += iSum becomes 1 and I becomes 2 to enter the next loop
  • When I =2 enters the if loop, which satisfies the if condition (I is even), continue, leaving the rest unexecutedsum += 1I becomes 3 to go to the next cycle
  • When I =3 enters the if loop and the if condition is not met (I is odd), executesum += iSum becomes 1+3=4 and I becomes 4 to enter the next loop
  • When I =4 enters the if loop just enough to satisfy the if condition (I is even), continuesum += 1I becomes 5 to go to the next cycle

From the first four steps, we found that the sum is only performed when I is odd, so it is actually the sum of the odd numbers from 1 to 100, which is exactly 2500

Double-layer for loop -break

Example 10:

for i in range(3) :# I: 0, 1, 2
    for j in range(4) :0, 1, 2, 3
        if j > i:
            break
        print((i,j))  Align # with if
        
# the results
(0.0)
(1.0)
(1.1)
(2.0)
(2.1)
(2.2)
Copy the code

Explain the results above:

The code above contains two for loops, entering the first loop from I =0:

i j Whether to enable if to execute break Whether to execute print
0 0 no Yes, 0,0.
0 1 is no
0 2 skipped
0 3 skipped
1 0 no Yes, comma 1,0.
1 1 no Yes, (1,1)
1 2 skipped
1 3 skipped
2 0 no Yes, 2 comma 0.
2 1 no Yes, 2 comma 1.
2 2 no Yes, 2 comma 2.
2 3 skipped

This is the execution and interpretation of the whole process

Example 11:

for i in range(3) :# I = 0
    for j in range(4) :# j = 0,1,2,3
        if j > i:
            break
    print((i,j))  # align with the second for
    
# the results
(0.1)
(1.2)
(2.3)
Copy the code

Explain the results above:

The difference between this example and the previous one is that print is aligned with the second for, which means that print is executed only when the second for has finished running

i j Whether to enable if to execute break Whether to execute print
0 0 no no
0 1 is Yes, 0,1.
0 2 skipped
0 3 skipped
1 0 no no
1 1 no no
1 2 is Yes, (1,2)
1 3 skipped
2 0 no no
2 1 no no
2 2 no no
2 3 is Yes, (2,3)

In other words, when two levels are looped, the break breaks out of the innermost (nearest break) for loop

Example 12:

for i in range(3) :for j in range(4) :if j > i:
            break
print((i,j))

# the results
(2.3)
Copy the code

The above result interpretation is the same as the previous two processes, until the last time (2,3) satisfies the break condition and print is executed.

Two-tier for loop -continue

Example 13:

for i in range(3) :# I = 0
    for j in range(4) :# j = 0,1,2,3
        if j > i:
            continue
        print((i,j))
        
# the results
(0.0)
(1.0)
(1.1)
(2.0)
(2.1)
(2.2)
Copy the code

Explain the results above:

i j Execute continue if if is satisfied Whether to execute print
0 0 no Yes, 0,0.
0 1 Is that out of
0 2 Is that out of
0 3 Is that out of
1 0 no Yes, comma 1,0.
1 1 no Yes, (1,1)
1 2 Is that out of
1 3 Is that out of
2 0 no Yes, 2 comma 0.
2 1 no Yes, 2 comma 1.
2 2 no Yes, 2 comma 2.
2 3 Is that out of

Example 14:

for i in range(3) :for j in range(4) :if j > i:
            continue
    print((i,j))
    
# the results
(0.3)
(1.3)
(2.3)
Copy the code

Explain the results above:

The print statement is indented, and print only happens when the second layer of for has been executed

i j Execute continue if if is satisfied Whether to execute print
0 0 no no
0 1 Is that out of no
0 2 Is that out of no
0 3 Is that out of Yes, 0,3.
1 0 no no
1 1 no no
1 2 Is that out of no
1 3 Is that out of Yes, (1,3)
2 0 no no
2 1 no no
2 2 no no
2 3 Is that out of Yes, (2,3)

Example: 15

for i in range(3) :for j in range(4) :if j > i:
            continue
print((i,j))

Take the value of the last time
(2.3)   
Copy the code

To summarize the essential differences: break is used to terminate the loop; Continue breaks out of the loop without affecting the execution of the next loop.

Artifact website

Finally gave everyone a classic artifact sites: pythontutor.com/visualize.h…

Let’s take this code as an example:

for i in range(3) :for j in range(4) :if j > i:
            continue
    print((i,j))
    
# the results
(0.3)
(1.3)
(2.3)
Copy the code

1. Write code

2. Demonstration process:

3. Specific process

When we run to step 15, the first result appears:

At the end of all 40 steps, we see the desired result:

Interested friends can go to this website to see ~