Tuyên bố Break đề cập đến cấp độ vòng bên trong nhất
Mã dưới đây là một vòng lặp vô hạn:
while True:
for i in range[10]:
if i == 5:
break # breaks the for, start a new iteration of the while loop
Để phá vỡ vòng lặp trong khi bạn có thể cân nhắc sử dụng một số loại cờ như thế này
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
Tuyên bố For-Else cũng có thể hữu ích ở đây:
while True:
for ...:
if ...:
# break the for loop
break # refers to the for statement
else:
# the breaking condition was never met during the for loop
continue # refers to the while statement
# this part only execute if the for loop was broken
break # refers to the while statement
Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về câu lệnh Python
2 và cách sử dụng nó để thoát khỏi vòng lặp sớm.: in this tutorial, you’ll learn about the Python while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 statement and how to use it to exit a loop prematurely.Giới thiệu về Tuyên bố Break Python
Đôi khi, bạn muốn chấm dứt vòng lặp
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
3 hoặc vòng lặp while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
4 sớm bất kể kết quả của các thử nghiệm có điều kiện. Trong những trường hợp này, bạn có thể sử dụng câu lệnh while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2:Code language: Python [python]
break
Thông thường, bạn sử dụng câu lệnh
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 với câu lệnh while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
7 để chấm dứt một vòng lặp khi một điều kiện là while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
8.Sử dụng Python Break với For Loop
Những điều sau đây cho thấy cách sử dụng câu lệnh
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 bên trong vòng lặp while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
3:Code language: Python [python]
for index in range[n]: # more code here if condition: break
Trong cú pháp này, nếu
while True:
for ...:
if ...:
# break the for loop
break # refers to the for statement
else:
# the breaking condition was never met during the for loop
continue # refers to the while statement
# this part only execute if the for loop was broken
break # refers to the while statement
1 đánh giá thành while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
8, câu lệnh while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 sẽ chấm dứt vòng lặp ngay lập tức. Nó đã thắng được thực hiện các lần lặp lại còn lại.Ví dụ này cho thấy cách sử dụng câu lệnh
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 bên trong vòng lặp while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
3:Code language: Python [python]
for index in range[0, 10]: print[index] if index == 3: break
Output:
Code language: Python [python]
0 1 2 3
Làm thế nào nó hoạt động.
- Vòng lặp
3 lặp lại hơn 10 số từ 0 đến 9 và hiển thị mỗi số trên màn hình.while True: broken = False for i in xrange[10]: if i == 5: broken = True # break the for loop break if broken: # break the while loop break
- Tuy nhiên, khi bộ đếm vòng là 3, câu lệnh
2 sẽ chấm dứt vòng lặp ngay lập tức. Do đó, chương trình chỉ hiển thị 4 số, từ 0 đến 3 trên màn hình.while True: broken = False for i in xrange[10]: if i == 5: broken = True # break the for loop break if broken: # break the while loop break
Khi bạn sử dụng câu lệnh
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 trong một vòng lặp lồng nhau, nó sẽ chấm dứt vòng lặp trong cùng. Ví dụ:Code language: Python [python]
for x in range[5]: for y in range[5]: # terminate the innermost loop if y > 1: break # show coordinates on the screen print[f"[{x},{y}]"]
Output:
Code language: Python [python]
[0,0] [0,1] [1,0] [1,1] [2,0] [2,1] [3,0] [3,1] [4,0] [4,1]
Ví dụ này sử dụng hai vòng
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
3 để hiển thị tọa độ từ 0 đếnCode language: Python [python]
break
1 trên màn hình.Code language: Python [python]
break
Tuyên bố
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 trong vòng lặp lồng nhau chấm dứt vòng lặp trong cùng khi 3 lớn hơn một.Code language: Python [python]
break
Do đó, bạn chỉ thấy các tọa độ có giá trị y bằng không và một.
Sử dụng câu lệnh Python Break với một vòng lặp lại
Sau đây cho thấy cách sử dụng câu lệnh
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 bên trong vòng lặp while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
4:Code language: Python [python]
while condition: # more code if condition: break
Ví dụ sau đây sử dụng câu lệnh
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
2 bên trong vòng lặp while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
4.Nó sẽ nhắc bạn vào màu sắc yêu thích của bạn. Chương trình sẽ dừng lại khi bạn nhập
8:Code language: Python [python]
break
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
0Output:
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
1Làm thế nào nó hoạt động.
- Vòng lặp
3 lặp lại hơn 10 số từ 0 đến 9 và hiển thị mỗi số trên màn hình.while True: broken = False for i in xrange[10]: if i == 5: broken = True # break the for loop break if broken: # break the while loop break
- Tuy nhiên, khi bộ đếm vòng là 3, câu lệnh
2 sẽ chấm dứt vòng lặp ngay lập tức. Do đó, chương trình chỉ hiển thị 4 số, từ 0 đến 3 trên màn hình.while True: broken = False for i in xrange[10]: if i == 5: broken = True # break the for loop break if broken: # break the while loop break
- Khi bạn sử dụng câu lệnh
2 trong một vòng lặp lồng nhau, nó sẽ chấm dứt vòng lặp trong cùng. Ví dụ:while True: broken = False for i in xrange[10]: if i == 5: broken = True # break the for loop break if broken: # break the while loop break
Ví dụ này sử dụng hai vòng while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
3 để hiển thị tọa độ từ break
Code language: Python [python]
0 đến break
Code language: Python [python]
1 trên màn hình.
while True:
broken = False
for i in xrange[10]:
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
break
break
- Tuyên bố
2 trong vòng lặp lồng nhau chấm dứt vòng lặp trong cùng khiwhile True: broken = False for i in xrange[10]: if i == 5: broken = True # break the for loop break if broken: # break the while loop break
3 lớn hơn một.
Code language: Python [python]break
Do đó, bạn chỉ thấy các tọa độ có giá trị y bằng không và một.