python:第二十九章:continue语句
一,continue语句
用途:
continue用于跳过当前循环中剩余的代码,直接进入下一次循环的执行。
当程序执行到 continue语句时,它会立即终止本次循环的执行,
并跳到下一次循环的开始处。
应用场景:
在本次循环中不需要执行循环体而需要跳过本次循环时使用
注意和break的区别:
是跳过本次循环开次下一次循环,(continue)
而不是结束整个循环去执行循环结束后的代码(break)
二,for循环中使用continue
|
1
2
3
4
5
|
# 打印从1到20,但跳过5的倍数for index in range(1, 21): if index % 5 == 0: continue print(index, end=" ") |
运行结果:
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/17/python-di-er-shi-jiu-zhang-continue-yu-ju/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
三,while循环中使用continue
|
1
2
3
4
5
6
7
|
# 打印1-20之间的奇数,跳过偶数counter = 0while counter < 20: counter += 1 if counter % 2 == 0: continue print(counter, end=" ") |
运行结果:
1 3 5 7 9 11 13 15 17 19

浙公网安备 33010602011771号