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