16.Python - continue & break & pass

跳出循环

True and False ,当输入1时,a=False时,会执行接下来的语句后再跳出这个循环。


a=True

while a:

    b= input('type somesthing')

    if b=='1':

        a= False

    else:

        pass

print ('finish run')


''''

type somesthing:2

still in while

type somesthing:3

still in while

type somesthing:1

still in while    #会执行下面的语句再跳出

finish run

''''


break

break语句和 C 中的类似,用于跳出最近的 for 或 while 循环。

while True:

    b= input('type somesthing:')

    if b=='1':

        break

    else:

        pass

    print('still in while')

print ('finish run')


"""

type somesthing:4

still in while

type somesthing:5

still in while

type somesthing:1

finish run

"""


continue

continue语句,也是从C语言借来的,表示继续下一次迭代:

在代码中,满足b=1的条件时,因为使用了 continue , python 不会执行 else 后面的代码,而会直接进入下一次循环。

while True:

    b=input('input somesthing:')

    if b=='1':

       continue

    else:

        pass

    print('still in while' )

print ('finish run')


"""

input somesthing:3

still in while

input somesthing:1  # 没有"still in while"。直接进入下一次循环

input somesthing:4

still in while

input somesthing:

"""


pass

pass 语句什么也不做。当语法上需要语句但程序不需要动作时,可以使用它。例如:

>>> while True:
...     pass  # Busy-wait for keyboard interrupt (Ctrl+C)
...










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值