1.continue 语句

continue 语句用于跳过当前循环的剩余语句,然后继续进行下一轮循环。它不像 break 语句那样跳出整个循环,而是跳过当前循环的剩余部分,继续进行下一轮循环。

continue 语句用法如下:

12345678
Python
for letter in 'Python':
    if letter == 'h':
        continue
    print(' 当前字母 :', letter)

print("Good bye!")
Plain text

以上实例输出结果为:

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : o
当前字母 : n
Good bye!
Plain text

2.break 语句

break 语句可以跳出 for 和 while 的循环体。如果你从 for 或 while 循环中终止,任何对应的循环 else 块将不执行。

break 语句用法如下:

12345678910
Python
for letter in 'Python':
    if letter == 'h':
        break
    print(' 当前字母 :', letter)

print("Good bye!")
Plain text

以上实例输出结果为:

当前字母 : P
Good bye!
Plain text

3. 总结

总的来说,continue 语句用于跳过当前循环的剩余语句,然后继续进行下一轮循环,而 break 语句可以跳出 for 和 while 的循环体,终止循环。