While loops
Loop while a condition is true
- A
whileloop repeats as long as its condition is true. - It checks the condition before each pass.
- Use it when you do not know the number of repeats in advance.
Lặp khi điều kiện còn đúng
- Một vòng lặp
whilelặp lại miễn là điều kiện của nó đúng. - Nó kiểm tra điều kiện trước mỗi lần lặp.
- Sử dụng khi bạn không biết trước số lần lặp.
Always change the condition
- Something inside the loop must move toward making the condition false.
- If it never becomes false, the loop runs forever (an infinite loop).
- Press Stop if a loop ever gets stuck.
Luôn thay đổi điều kiện
- Có một thứ gì đó bên trong vòng lặp phải tiến tới việc làm cho điều kiện trở thành sai.
- Nếu nó không bao giờ trở thành sai, vòng lặp sẽ chạy mãi mãi (một vòng lặp vô hạn).
- Nhấn Dừng nếu vòng lặp bị kẹt bất cứ lúc nào.
count = 3
while count > 0:
print(count)
count = count - 1
break and continue
breakstops the loop right away.continueskips the rest of this pass and goes to the next one.while True:loops forever until abreakends it.
break và continue
breakdừng vòng lặp ngay lập tức.continuebỏ qua phần còn lại của lần lặp hiện tại và chuyển sang lần tiếp theo.while True:lặp mãi mãi cho đến khi mộtbreakkết thúc nó.
n = 0
while True:
n = n + 1
if n == 4:
break
print(n)
Checking before or after
- A
whileloop checks first, so it may run zero times. - A "repeat until" loop checks after, so it always runs at least once.
- Python has no
repeat; usewhile True:with abreakat the end.
Kiểm tra trước hay sau
- Một vòng lặp
whilekiểm tra trước, nên nó có thể chạy không lần nào. - Một vòng lặp "lặp cho đến khi" kiểm tra sau, nên nó luôn chạy ít nhất một lần.
- Python không có
repeat; hãy sử dụngwhile True:với mộtbreakở cuối.
In Cambridge pseudocode
whileis the pre-condition loop;REPEAT … UNTILis the post-condition loop.
Trong pseudocode Cambridge
whilelà vòng lặp điều kiện trước;REPEAT … UNTILlà vòng lặp điều kiện sau.
Count ← 3
WHILE Count > 0
OUTPUT Count
Count ← Count - 1
ENDWHILE
REPEAT
OUTPUT Count
Count ← Count + 1
UNTIL Count = 3
Common mistakes
- Change something inside the loop so the condition can become false, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
Lỗi thường gặp
- Thay đổi thứ gì đó bên trong vòng lặp để điều kiện có thể trở thành sai, nếu không nó sẽ chạy mãi mãi.
- Một
whilekiểm tra điều kiện trước, vì vậy nó có thể chạy 0 lần.
Now you try
- Make sure your condition will eventually become false.
- Stuck on the loop? Do the short drill Finish the countdown first. It is one missing line.
- Press Check answer to test your code.
Bây giờ bạn thử
- Đảm bảo rằng điều kiện của bạn cuối cùng sẽ trở thành sai.
- Bị kẹt ở vòng lặp? Hãy làm bài luyện tập ngắn Hoàn thành bộ đếm ngược trước. Đó là một dòng thiếu.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
A while loop builds up a value
A while loop repeats until its condition is false.
Use a while loop to count down. Each turn, print the value of count, then make count one smaller. Stop when it reaches 0, so the output is 5, 4, 3, 2, 1 — one per line.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Use a while loop to add up the numbers 1 to 10. Store the total in total (the answer is 55).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Start n at 1 and add 1 each pass. The loop stops too soon: it ends while n is still 1. Change the condition so it keeps going while n * n is at most 50. When it stops, n is the first whole number whose square is over 50.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Same answer, with break. n starts at 1 inside while True:. The if is already inside the loop and n grows after it. Change the condition so you break when n * n is greater than 50. Leave the answer in n (it is 8). Do not add 1 before the test — that makes the answer 9.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Start with n = 1 and steps = 0. While n is smaller than 1000, double n and add 1 to steps. When the loop ends, n is 1024. How many doublings did it take? Leave the answer in steps.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.