Python 循环控制语句详解:break、continue、pass 实战示例
循环是大多数编程语言的骨干,支持对列表、元组、字符串、字典、集合等序列重复操作。Python 提供 for 和 while 两种循环构造。循环持续直到条件为假,但可用 break、continue、pass 改变流程。
什么是循环控制语句?
- break – 立即退出所在循环。
- continue – 跳过本次迭代剩余部分,进入下一轮。
- pass – 空操作;语法要求必须有语句块时的占位符。
这些语句赋予开发者对循环执行的精细控制,让代码更高效、可读。
break 语句
break 终止其所在的最内层循环。嵌套循环中仅影响当前循环,外层继续运行。
break 工作流程(流程图)

流程:
- 进入循环。
- 评估循环条件。
- 遇到
break则退出循环。 - 否则执行循环体。
- 重新评估条件,重复直到条件失败。
示例
for 循环中 break
my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']
for name in my_list:
print(name)
if name == 'Guru':
print('Found the name Guru')
break
print('Loop is terminated')
输出:
Siya
Tiya
Guru
Found the name Guru
Loop is terminated
while 循环中 break
my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']
index = 0
while True:
print(my_list[index])
if my_list[index] == 'Guru':
print('Found the name Guru')
break
index += 1
print('After while-loop exit')
输出:
Siya
Tiya
Guru
Found name Guru
After while-loop exit
嵌套循环中 break
for i in range(4):
for j in range(4):
if j == 2:
break
print("The number is", i, j)
输出:
The number is 0 0
The number is 0 1
The number is 1 0
The number is 1 1
The number is 2 0
The number is 2 1
The number is 3 0
The number is 3 1
continue 语句
continue 跳过当前迭代剩余代码,直接进入下一轮循环。
continue 工作流程(流程图)

示例
for 循环中 continue
for i in range(10):
if i == 7:
continue
print("The Number is:", i)
输出:
The Number is: 0
The Number is: 1
The Number is: 2
The Number is: 3
The Number is: 4
The Number is: 5
The Number is: 6
The Number is: 8
The Number is: 9
while 循环中 continue
i = 0
while i <= 10:
if i == 7:
i += 1
continue
print("The Number is:", i)
i += 1
输出:
The Number is: 0
The Number is: 1
The Number is: 2
The Number is: 3
The Number is: 4
The Number is: 5
The Number is: 6
The Number is: 8
The Number is: 9
The Number is: 10
嵌套循环中 continue
for i in range(4):
for j in range(4):
if j == 2:
continue
print("The number is", i, j)
输出:
The number is 0 0
The number is 0 1
The number is 0 3
The number is 1 0
The number is 1 1
The number is 1 3
The number is 2 0
The number is 2 1
The number is 2 3
The number is 3 0
The number is 3 1
The number is 3 3
pass 语句
pass 是无操作占位符,语法要求必须有语句块但暂无动作时使用。常用于空函数体、类、循环、条件块的存根。
何时使用 pass
- 定义待实现的存根函数或类。
- 临时禁用代码块但保留语法结构。
- 调试时提供最小循环/条件体。
示例
函数中 pass
def my_func():
print('pass inside function')
pass
my_func()
输出:pass inside function
类中 pass
class My_Class:
print("Inside My_Class")
pass
输出:Inside My_Class
循环中 pass
# for 循环中 pass
text = "Guru"
for char in text:
if char == 'r':
print('Pass executed')
pass
print(char)
输出:
G
u
Pass executed
r
u
if 语句中 pass
a = 1
if a == 1:
print('pass executed')
pass
输出:pass executed
break 与 continue 选择指南
- break – 需基于条件完全退出循环时使用。
- continue – 需跳过本轮剩余但继续循环时使用。
理解细微差别确保循环高效运行且行为符合预期。
核心要点
- 用
break在条件满足时终止循环。 - 用
continue跳过当前迭代剩余部分。 - 用
pass作为未来代码占位或满足语法要求。
Python
- C# 表达式、语句与代码块:全面指南与实战示例
- C++ do-while 循环详解:语法、实战示例与嵌套循环
- 精通Python Pass语句:实战指南
- Python print() 函数完全指南:基础用法、空行打印与自定义结束符
- Python 循环完全指南:for、while、break、continue、enumerate 详解
- Python str.count() 完全指南:统计字符与子串出现次数实战
- Python format() 字符串格式化完全指南:占位符、格式化规范、类与字典应用
- Python 字符串 find() 方法完全指南:语法、参数、替代方案与计数技巧
- 精通 Python Lambda 函数:实用示例与最佳实践
- Python round() 函数详解与实战示例
- 精通 Python range() 函数:从基础到高级用法
- 精通 Python map() 函数:语法、示例与最佳实践