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

Python中的break语句相关推荐

  1. python中的for语句涉及的序列可以是列表_Python中的列表与循环

    文章中的所有例子全部经过实际测试,可以直接使用.开发环境是python 3.8.5 条件和循环 本章的主要内容是Python的条件和循环语句.主要涉及if.else.elif.while.for.ra ...

  2. python循环语句-详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  3. Python基础:break语句知识详解

    欢迎你来到站长在线的站长学堂学习Python知识,今天起把教程分开来拆解知识点,把每一个知识点讲透.比如今天要讲的课程内容是<零基础Python完全自学教程13:Python中的break语句. ...

  4. python的for语句-详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  5. Python中if判断语句在只有一个break子句时可以写在一行

    1 致谢 感谢北京理工大学嵩天和黄天羽等老师的辛勤付出 链接如下: http://www.icourse163.org/learn/BIT-268001?tid=1002235009#/learn/a ...

  6. python循环 break举例,python循环中使用break语句终止循环

    break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句. 一个重要的注释是,如果你从for或while循环中 终止 ,任何对应的循环else ...

  7. Python中的break、continue、pass和循环else语句

    Python中的break.continue.pass语句 一.基本介绍和一般循环格式 二.pass 三.continue 四.break 五.循环else 一.基本介绍和一般循环格式 本文是在whi ...

  8. Python中的判断语句及循环

    Python中的判断语句及循环 文章目录 Python中的判断语句及循环 一.判断(if)语句 01. 开发中的应用场景 程序中的判断 判断的定义 02. if 语句体验 2.1 if 判断语句基本语 ...

  9. python中while语句是_如何在Python中使用while语句[适合初学者]

    while语句是重复循环的语句,那么如何用Python编写,下面Gxl网就带领大家来学习一下Python中使用while语句.[推荐阅读:Python视频教程] 一:什么是while语句?Python ...

最新文章

  1. gin自定义路由日志的格式
  2. 数据结构: 线索化二叉树
  3. 给自己一点恒心,加油打气~
  4. uImage和zImage的区别
  5. Apache Druid(一)简介
  6. 上海交通大学计算机应用基础答案,西安交通大学17年3月课程考试《计算机应用基础》作业考核试题答案...
  7. SAP License:作业费用分割均分常见原因
  8. jdk12连接mysql_使用基于JDK12版本的JDBC读取数据库中的数据在网页(jsp)表示出来...
  9. c# 操作FTP文件类
  10. 教妹学Java(十):Unicode字符集简介
  11. 计算机网络是由网络硬件网络软件,计算机网络系统主要由网络硬件系统和网络软件系统组成。...
  12. 长城脚下公社之凯宾斯基开业典礼
  13. c语言实现万能求积分
  14. python语言必刷题——BMI值的计算
  15. 数据分析师的发展前景
  16. KEIL MDK RTX 实时操作系统
  17. Mysql日期的加减法_mysql日期加减
  18. 从海外客户端Bidding看,移动开发者应如何应对竞价模式变化?
  19. 重点人口动态管控轨迹分析系统开发,可视化大数据平台建设
  20. C语言界杠把子的书籍,你读过几本?

热门文章

  1. 远程连接基于VMware虚拟机的linux操作系统
  2. 快捷键大全---windows , idea , linux , 浏览器
  3. java基础--Java入门
  4. JDK自带的反编译工具 javap
  5. Flink JobManager占用注册端口引起的小问题
  6. HaploMerger2: 从高杂合二倍体基因组组装中重建单倍型
  7. 优品商城-建表(user、member-goods、goods_cart、category、order、spec-address、province、city、county)
  8. mavlink协议_MAVLink学习之路03_XML中定义MSG并生成C代码
  9. 论“正义”——什么是正义?
  10. html使用thymeleaf模板时,获取数据库中字符串值,拆分为list根据下标获取对应的值的方法