While loop, the difference between break and continue

continue returns to the beginning of the loop and executes the loop body

break directly out of the loop

Whether it is continue or break, the code below them will no longer be executed in this loop

E.g:

i = 0
while i < 5:
    i += 1
    if i == 1:
        continue

    print(i)

The output is

2

3

4

5

There is no output in the case of i=1, because when i=1, print(i) is not executed

while the following

while i < 5:
    i += 1
    if i == 1:
        break

    print(i)

The output result is nothing, because when i=1, it jumps out of the loop directly, and print(i) will not execute

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324471707&siteId=291194637