break:立刻退出整个循环
%执行break时,while循环终止
x=1
while x<10:
if x == 6:
break
x += 1
print(x)
结果:6
%执行break时,for循环终止
str = "happy"
for c in str:
if c == 'p':
break
else:
print("Letter is",c)
结果:
Letter is h
Letter is a
continue:停止当前正在执行的循环语句
%执行continue时,只终止if循环
for n in range(4):
if n==1:
continue
print(n)
结果:
0
2
3
str = "happy"
for c in str:
if c == 'p':
continue
else:
print("Letter is",c)
结果:
Letter is h
Letter is a
Letter is y