深度阅读

How to use the `continue` statement in a `while` loop in Python

作者
作者
2023年08月22日
更新时间
8.69 分钟
阅读时间
0
阅读量

To use the continue statement in a while loop in Python, you can add an if statement within the loop to check for a certain condition. If the condition is met, you can use the continue statement to skip the current iteration and continue to the next iteration of the loop. Here is an example:

i = 0
while i < 10:
    i += 1
    if i == 5:
        continue
    print(i)

In this example, the while loop iterates through the numbers 1 to 10. If the current value of i is equal to 5, the continue statement is executed, which skips the current iteration of the loop and continues to the next iteration. This means that the number 5 will be skipped in the output.

The output of this program will be:

1
2
3
4
6
7
8
9
10

Note that the continue statement can be used in any type of loop in Python, including for loops and nested loops.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。