Python 简明教程

Python - break Statement

Python break Statement

Python break 语句用于终止当前 loop ,并在下一语句处恢复执行,就像传统的 break statement in C

Python break statement is used to terminate the current loop and resumes execution at the next statement, just like the traditional break statement in C.

Python break 语句最常见的使用方式是当引发一些外部条件并要求突然退出循环时。break 语句既可用于 Python while 循环,也可用于 for 循环。

The most common use for Python break statement is when some external condition is triggered requiring a sudden exit from a loop. The break statement can be used in both Python while and for loops.

如果你使用 nested loops in Python ,break 语句将停止执行最内层循环,并在块之后开始执行下一行代码。

If you are using nested loops in Python, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.

Syntax of break Statement

Python 中 break 语句的语法如下 −

The syntax for a break statement in Python is as follows −

looping statement:
   condition check:
      break

Flow Diagram of break Statement

以下是 break 语句的流程图 −

Following is the flowchart of the break statement −

cpp break statement

break Statement with for loop

如果我们在 for 循环内使用 break 语句,它会中断程序的正常流程,并在完成迭代之前退出循环。

If we use break statement inside a for loop, it interrupts the normal flow of program and exit the loop before completing the iteration.

Example

在本示例中,我们将看到 break 语句在 for 循环中的工作原理。

In this example, we will see the working of break statement in for loop.

for letter in 'Python':
   if letter == 'h':
      break
   print ("Current Letter :", letter)
print ("Good bye!")

执行上述代码后,将生成以下结果 −

When the above code is executed, it produces the following result −

Current Letter : P
Current Letter : y
Current Letter : t
Good bye!

break Statement with while loop

与 for 循环类似,我们可以在指定条件变为 TRUE 之后使用 break 语句跳过 while 循环内的代码。

Similar to the for loop, we can use the break statement to skip the code inside while loop after the specified condition becomes TRUE.

Example

下面的代码展示了如何使用 break(中断)语句与 while(当且仅当)循环组合使用。

The code below shows how to use break statement with while loop.

var = 10
while var > 0:
   print ('Current variable value :', var)
   var = var -1
   if var == 5:
      break

print ("Good bye!")

执行以上代码,它将产生如下结果:

On executing the above code, it produces the following result −

Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!

break Statement with Nested Loops

嵌套循环中,一个循环定义在另一个循环中。包含另一个循环(即:内循环)的循环称为外循环。

In nested loops, one loop is defined inside another. The loop that enclose another loop (i.e. inner loop) is called as outer loop.

当我们在嵌套循环中使用 break(中断)语句时,它的行为如下:

When we use a break statement with nested loops, it behaves as follows −

  1. When break statement is used inside the inner loop, only the inner loop will be skipped and the program will continue executing statements after the inner loop

  2. And, when the break statement is used in the outer loop, both the outer and inner loops will be skipped and the program will continue executing statements immediate to the outer loop.

Example

下面的程序展示了如何在 for(对于)循环中使用 break(中断)来遍历 list 。在这里,将会在列表中搜索指定的数字。如果找到了,那么循环将会打印“found”消息并终止。

The following program demonstrates the use of break in a for loop iterating over a list. Here, the specified number will be searched in the list. If it is found, then the loop terminates with the "found" message.

no = 33
numbers = [11,33,55,39,55,75,37,21,23,41,13]
for num in numbers:
   if num == no:
      print ('number found in list')
      break
else:
   print ('number not found in list')

以上程序将产生以下 output

The above program will produce the following output

number found in list