操作系统:window7 x64
编程IDE:Pycharm 2016.1.2
Python版本:3.6.1
编辑时间:2017年4月21日
版权所有:OE, 转载请注明出处:http://blog.csdn.net/csnd_ayo
英文原文
A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there is no next item.
中文译文
用 break
关键字终止当前循环就不会执行当前的 else
语句,而使用 continue
关键字快速进入下一论循环,或者没有使用其他关键字,循环的正常结束后,就会触发 else
语句。
正常结束的循环
list = [1,2,3,4,5]
for x in list:
print(x)
else:
print("else")
使用 continue 关键字
list = [1,2,3,4,5]
for x in list:
continue
print(x)
else:
print("else")
list = [1,2,3,4,5]
for x in list:
print(x)
break
else:
print("else")
for else语句可以总结成以下话。
如果我依次做完了所有的事情(for正常结束),我就去做其他事(执行else),若做到一半就停下来不做了(中途遇到break),我就不去做其他事了(不执行else)。
else
。break
可以阻止 else
语句块的执行。