Python中的break语句

Python break语句 (Python break statement)

Like other programming languages, in python, break statement is used to terminate the loop's execution. It terminates the loop's execution and transfers the control to the statement written after the loop.

与其他编程语言一样 ,在python中, break语句用于终止循环的执行。 它终止循环的执行并将控制权转移到循环之后编写的语句。

If there is a loop inside another loop (nested loop), and break statement is used within the inner loop – it breaks the statement of the inner loop and transfers the control to the next statement written after the inner loop. Similarly, if break statement is used in the scope of the outer loop – it breaks the execution of the outer loop and transfers the control to the next statement written after the outer loop.

如果另一个循环(嵌套循环)中有一个循环,并且在内部循环中使用break语句 –它将中断内部循环的语句并将控制权转移到内部循环之后编写的下一个语句。 同样,如果在外部循环的范围内使用break语句 –它将中断外部循环的执行,并将控制权转移到在外部循环之后编写的下一个语句。

Syntax of break statement:

break语句的语法:

    break

Break语句的Python示例 (Python examples of break statement )

Example 1: Here, we are printing the serious of numbers from 1 to 10 and terminating the loop if counter’s value is equal to 7.

示例1:在这里,我们打印从1到10的严重数字,并且如果counter的值等于7,则终止循环。

# python example of break statement

counter = 1

# loop for 1 to 10
# terminate if counter == 7

while counter<=10:
	if counter == 7:
		break
	print(counter)
	counter += 1

print("outside of the loop")

Output

输出量

1
2
3
4
5
6
outside of the loop

Example 2: Here, we are printing the characters of a string and terminating the printing of the characters if the character is not an alphabet.

示例2:在这里,我们正在打印字符串的字符,如果字符不是字母,则终止字符的打印。

# python example of break statement

string = "IncludeHelp.Com"

# terminate the loop if 
# any character is not an alphabet 

for ch in string:
    if not((ch>='A' and ch<='Z') or (ch>='a' and ch<='z')):
        break
    print(ch)

print("outside of the loop")    

Output

输出量

I
n
c
l
u
d
e
H
e
l
p
outside of the loop

Example 3: Here, we have two loops, outer loop is "while True:" which is handling the multiple inputs and inner loop is using to print the table of given number – as input is 0 – inner loop will be terminated.

示例3:在这里,我们有两个循环,外循环是“ while True:”,它处理多个输入,而内循环则用于打印给定编号的表–输入为0 –内循环将终止。

# python example of break statement

count = 1
num = 0
choice = 0

while True:
    # input the number 
    num = int(input("Enter a number: "))
    
    # break if num is 0
    if num==0:
        break   # terminates inner loop
    
    # print the table 
    count = 1
    while count<=10:
        print(num*count)
        count += 1

    # input choice
    choice = int(input("press 1 to continue..."))
    if choice != 1:
        break   # terminates outer loop

print("bye bye!!!")

Output

输出量

Enter a number: 21
21 
42 
63 
84 
105
126
147
168
189
210
press 1 to continue...1
Enter a number: 23
23 
46 
69 
92 
115
138
161
184
207
230
press 1 to continue...0
bye bye!!!


翻译自: https://www.includehelp.com/python/break-statement.aspx

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值