Skip to content

[Python] Tutorial (4) break, pass, continue

Last Updated on 2021-04-07 by Clay

before we teaching the concepts of "break", "pass", "continue" ......, I think I have to complete the lack of "while-loop".

The "while", we can think of it as a "loop that continues to execute as long as it meets the conditions."


Let me give you a basic example:

while True:
    print('no')


If you execute the program... Oh No! That't terrible. Until you terminate the program, it will run to print no in the loop.

The scene is definitely bad.

The above is one of the most basic while loops, let's take a look at another example.

a = 3
while a == 3:
    print('no')


If you execute this program, will it always print the same as before?

The answer is YES! After the while is the condition of the while execution, as long as the conditions are met, the block indented under the while will be executed all the time!

So how exactly does the while-loop control the process?

Our protagonist today: break, pass, continue is coming!

Of course, you can use the process control in the for-loop.


Break

When break appears in the iterative program (such as for-loop and while-loop), it means that we exited directly out of this loop.

We set a variable n and then we will print the variable —— until the variable equals 10, we use the command "break" to leave the loop.

n = 0
while True:
    n += 1
    print(n)

    if n == 10:
        break


Output:

1
2
3
4
5
6
7
8
9
10

Is the loop closed after n prints equal to 10?


Pass

The command "Pass" is relatively simple, we can think of it as "skip" an iteration does not execute in its break.

Let's look at a simple example: Let's let n pass once when it equals 4, what effect will this have on our printed numbers?

for n in range(10):
    if n == 4:
        pass
    else:
        print(n)


Output:

1
2
3
5
6
7
8
9

Look! Did you skip 4?


Continue

For continue, his usage is very similar to pass, skipping the current iteration.

But instead of skipping the code of your own block, continue will stop this iteration directly, starting from the next loop.

May be we take a look at the comparison of the sample code:

# pass 
times = 0
for n in range(10):
    if n == 4:
        pass
    else:
        print(n)

    times += 1
    print('we run %d times' % times)


Output:

 0
 we run 1 times 
 1
 we run 2 times 
 2
 we run 3 times 
 3
 we run 4 times 
 we run 5 times 
 5
 we run 6 times 
 6
 we run 7 times 
 7
 we run 8 times 
 8
 we run 9 times 
 9
 we run 10 times 

We use a new variable times as a counter. We can see that although we skipped the part printed by n when n = 4, the part under the loop is executed as usual, and the pass only skip its own. The code of the area.

# continue
times = 0
for n in range(10):
    if n == 4:
        continue
    else:
        print(n)

    times += 1
    print('we run %d times' % times)


Output:

 0
 we run 1 times 
 1
 we run 2 times 
 2
 we run 3 times 
 3
 we run 4 times 
 5
 we run 5 times 
 6
 we run 6 times 
 7
 we run 7 times 
 8
 we run 8 times 
 9
 we run 9 times 
 

We can clearly see that this time using continue, we even break out of the printout part of the bottom! It means that continue is to suspend this loop, and continue from the next iteration.

These are the notes on the basic tutorial of Python today.

You may wish to refer to other articles on my blog ~ Thanks ~


References


Read More

Leave a Reply