The difference between python break and continue

1. The difference between break and continue

break and continueare keywords used to control the execution of loop statements. The difference is:

  • break It will terminate the current loop, no longer execute the remaining code of the current loop body, exit the current loop body, and then continue to execute the code outside the loop.
  • continueWill stop the current iteration, skip the remaining statements of this loop, and then continue with the next iteration. .

Here is a simple example illustrating the difference between break and : continue

# 示例代码
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# 使用 break
for n in numbers:
    if n == 5:
        break
    print(n)

# 输出结果:1 2 3 4

# 使用 continue
for n in numbers:
    if n == 5:
        continue
    print(n)

# 输出结果:1 2 3 4 6 7 8 9 10

In the above example, we used two loop statements to iterate over a list of numbers from 1 to 10 . In the first loop, we use the break  keyword to stop the loop and jump out of the loop body. Therefore, when the loop reaches n  equals 5, the loop stops, the subsequent statements are no longer executed, and it jumps directly to the code outside the loop. In the second loop, we use the continue  keyword to stop this iteration, skip the remaining statements of this loop, and start the next iteration directly. Therefore, when the loop reaches n  equals 5, this iteration stops and the next iteration starts directly, without executing the remaining statements of this iteration.

You can also use the flow chart below to explain their differences.

2. Break and continue are applied in while and for loops

In order to clearly understand the application of break and continue in while and for loops, two cases will be used to illustrate .

Case 1:

Randomly generate a number between [1-10], and have 3 chances to guess the generated random number. During the guessing process, you can guess the big number. If you guess the small number or the guess is correct, you will be given a prompt, and the guess will stop immediately.

break is used in a while loop:

import random

num = random.randint(1, 10)  # [1,10] 闭区间
print(f"生成随机数为{num}")

count = 1
while count <= 3:
    # input输出为str,需要进行数据转换int,才可以进行比较.
    input_id = int(input("请输入[1-10]数字:")) 
    #对输入的数字范围进行初始判断
    if 1 <= input_id <= 10:
        if input_id == num:
            print("恭喜贺喜,你猜对了!")
            break  # 跳出循环
        else:
            if input_id < num:
                print(f"不好意思,你猜小了!")
            else:
                print(f"不好意思,你猜大了!")
            #次数判断
            if count ==3:
                print("你的次数已经用完!!!")
                break  # 跳出循环
            else:
                print(f"你还有{3-count}次机会!!!")
    else:
        print(f"你输入的[{input_id}]不在[1-10]内")
    count += 1
print(f"你总共猜了{count}次")

break is used in a for loop:

import random

num = random.randint(1, 10)  # [1,10] 闭区间
print(f"生成随机数为{num}")

count = 1
for i in range(3):
    # input输出为str,需要进行数据转换int,才可以进行比较.
    input_id = int(input("请输入[1-10]数字:"))
    #对输入的数字范围进行初始判断
    if 1 <= input_id <= 10:
        if input_id == num:
            print("恭喜贺喜,你猜对了!")
            break  # 跳出循环
        else:
            if input_id < num:
                print(f"不好意思,你猜小了!")
            else:
                print(f"不好意思,你猜大了!")
            #次数判断
            if count ==3:
                print("你的次数已经用完!!!")
                break  # 跳出循环
            else:
                print(f"你还有{3-count}次机会!!!")
    else:
        print(f"你输入的[{input_id}]不在[1-10]内")
    count += 1
print(f"你总共猜了{count}次")

Running one of them yields the following results: 

Case 2:

Print out all odd numbers between 1 and 10.

continue is used in a while loop: 

i = 1
while i <= 10:
    if i % 2 == 0:
        # 如果 i 是偶数,跳过当前迭代
        i += 1
        continue
    print(i)
    i += 1

 continue is used in a for loop: 

for i in range(1, 11):
    # 如果 i 是偶数,跳过当前迭代
    if i % 2 == 0:
        continue
    print(i)

The following is the result of running

Guess you like

Origin blog.csdn.net/tjfsuxyy/article/details/130229260