break is a keyword in python just like another programming language and it is used to break the execution of loop statement.
就像另一種編程語言一樣, break是python中的關鍵字,用于中斷loop語句的執行。
In the given example, loop is running from 1 to 10 and we are using the break statement if the value of i is 6. Thus when the value of i will be 6, program's execution will come out from the loop.
在給定的示例中,循環從1到10運行,并且如果i的值為6,則使用break語句。因此,當i的值為6時,程序的執行將從循環中執行。
Example 1:
范例1:
for i in range(1,11):
if(i==6):
break
print(i)
Output
輸出量
1
2
3
4
5
Example 2: In this example, we are printing character by character of the value/string “Hello world” and terminating (using break), if the character is space.
示例2:在此示例中,如果字符為空格,則按字符/值/字符串“ Hello world”打印字符并終止(使用break)。
for ch in "Hello world":
if ch == " ":
break
print(ch)
Output
輸出量
H
e
l
l
o
翻譯自: https://www.includehelp.com/python/break-statement-example.aspx