Python探索记(08)——break和continue

简介: 请看如下示例:# @Time : 2017/7/2 17:12# @Author : 原创作者:谷哥的小弟# @Site : 博客地址:http://blog.

请看如下示例:

# @Time    : 2017/7/2 17:12
# @Author  : 原创作者:谷哥的小弟
# @Site    : 博客地址:http://blog.csdn.net/lfdfhl
# @DESC    : break和continue
'''
利用break语句结束整个循环
'''
index=10
while index>0:
    if index==5:
        break
    print('index=',index)
    index=index-1
print('= '*15)
'''
利用continue结束本次循环再继续着执行下一次的循环
'''
name='大泽玛利亚'
for per in name:
    if per=='玛':
        continue
    print('per=',per)

输出结果如下:

index= 10
index= 9
index= 8
index= 7
index= 6
= = = = = = = = = = = = = = = 
per= 大
per= 泽
per= 利
per= 亚
相关文章
|
C语言 Python
Python break 语句
Python break 语句
|
Python
在Python中,`continue` 语句
在Python中,`continue` 语句
363 5
|
Python
在Python中,`break`语句
在Python中,`break`语句
320 1
|
12月前
|
Java C++ Python
【Python】循环语句(while、for)、continue、break
【Python】循环语句(while、for)、continue、break
246 1
|
程序员 Python
Python continue 语句
Python continue 语句
300 2
|
Python
Python中continue语句
Python中continue语句
278 2
Python基础教程——continue语句
Python基础教程——continue语句
Python基础教程——break语句
Python基础教程——break语句
|
Python
Python中break详解以及用法
`break`语句在Python中用于提前结束循环。当遇到`break`时,循环立即停止,程序跳至循环体外继续执行。它适用于`for`和`while`循环,常与条件判断结合,满足特定条件即中断循环。示例展示了在不同循环中使用`break`的情况。注意,`break`只能用于循环且仅终止最内层循环,会导致循环中的`else`语句不执行。它是控制程序流程的有效工具,但需谨慎使用。
882 1

热门文章

最新文章

推荐镜像

更多