亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 循环控制语句详解:break、continue、pass 实战示例

循环是大多数编程语言的骨干,支持对列表、元组、字符串、字典、集合等序列重复操作。Python 提供 for 和 while 两种循环构造。循环持续直到条件为假,但可用 break、continue、pass 改变流程。

什么是循环控制语句?

这些语句赋予开发者对循环执行的精细控制,让代码更高效、可读。

break 语句

break 终止其所在的最内层循环。嵌套循环中仅影响当前循环,外层继续运行。

break 工作流程(流程图)

Python 循环控制语句详解:break、continue、pass 实战示例

流程:

  1. 进入循环。
  2. 评估循环条件。
  3. 遇到 break 则退出循环。
  4. 否则执行循环体。
  5. 重新评估条件,重复直到条件失败。

示例

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 工作流程(流程图)

Python 循环控制语句详解:break、continue、pass 实战示例

示例

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 选择指南

理解细微差别确保循环高效运行且行为符合预期。

核心要点

Python

  1. C# 表达式、语句与代码块:全面指南与实战示例
  2. C++ do-while 循环详解:语法、实战示例与嵌套循环
  3. 精通Python Pass语句:实战指南
  4. Python print() 函数完全指南:基础用法、空行打印与自定义结束符
  5. Python 循环完全指南:for、while、break、continue、enumerate 详解
  6. Python str.count() 完全指南:统计字符与子串出现次数实战
  7. Python format() 字符串格式化完全指南:占位符、格式化规范、类与字典应用
  8. Python 字符串 find() 方法完全指南:语法、参数、替代方案与计数技巧
  9. 精通 Python Lambda 函数:实用示例与最佳实践
  10. Python round() 函数详解与实战示例
  11. 精通 Python range() 函数:从基础到高级用法
  12. 精通 Python map() 函数:语法、示例与最佳实践