python - break和continue
break
终止整个循环:当循环或判断执行到break语句时,即使判断条件为True或者序列尚未完全被历遍,都会跳出循环或判断
for i in xrange(10):
print(i)
if i == 8:
break
print('end')
执行:
C:\Python27\python.exe D:/Python/type-of-data.py
0
1
2
3
4
5
6
7
8
end
Process finished with exit code 0
continue
跳出当次循环
当循环或判断执行到continue语句时,continue后的语句将不再执行,会跳出当次循环,继续执行循环中的下一次循环
for i in xrange(10):
if i ==3:
print('lalalalalalala')
continue
print(i)
print('end')
执行:
C:\Python27\python.exe D:/Python/type-of-data.py
0
1
2
lalalalalalala
4
5
6
7
8
9
end
Process finished with exit code 0
总结:
continue 语句跳出本次循环,只跳过本次循环continue后的语句
break 语句跳出整个循环体,循环体中未执行的循环将不会执行
for i in xrange(10):
if i ==3:
print('lalalalalalala')
continue
print(i)
if i == 8:
break
print('end')
执行:
C:\Python27\python.exe D:/Python/type-of-data.py
0
1
2
lalalalalalala
4
5
6
7
8
end
Process finished with exit code 0