Vòng lặp while
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| while loop/waɪl luːp/ | vòng lặp while |
| loop control variable/luːp kənˈtrəʊl ˈveərɪəbl/ | biến điều khiển vòng lặp |
| infinite loop/ˈɪnfɪnət luːp/ | vòng lặp vô hạn |
Repeat while true
- A while loop 当型循环 repeats its body as long as a condition stays
true. while (i <= 5) { sum = sum + i; i++; }- Before each pass, Java checks the condition; if
false, the loop ends. - Use
whilewhen you don't know in advance how many passes you'll need.
Test-before-each-pass
- The condition is tested before the body runs each time.
- If it's
falseon the very first check, the body runs zero times. - After each pass, the condition is re-checked before the next.
- This "check, then maybe run" order is worth memorizing.

The three loop types differ in where the condition is tested
The loop control variable
- A loop control variable 循环控制变量 drives progress toward stopping.
- Initialize it before the loop, use it in the condition, and change it inside the body.
istarts at1, the condition checksi <= 5, andi++moves it forward.- Without a change that eventually breaks the condition, the loop never ends.
Infinite loops
- An infinite loop 无限循环 happens when the condition never becomes false.
- Forgetting
i++(or updating the wrong variable) traps the program forever. - The tell: the loop makes no progress toward its stopping condition.
- Always make sure each pass moves the control variable toward the exit.
A while checks its condition BEFORE each pass — so it can run zero times. If the condition is already false, the body never runs. And you must change the loop control variable inside the body so the condition eventually turns false; forgetting the update (e.g. no i++) is the classic infinite loop.
Summing 1 to 5:
int i = 1, sum = 0;while (i <= 5) { sum = sum + i; i++; }- Passes:
i=1sum=1,i=2sum=3,i=3sum=6,i=4sum=10,i=5sum=15; theni=6fails<= 5→ stop.sumis15.
A while loop repeats its body while a condition is true, checking before each pass (so it may run zero times). A loop control variable must be initialized, tested, and updated inside the body so the loop makes progress — forgetting the update causes an infinite loop.
Tính tổng từ 1 đến 5 với while
Mỗi lượt cộng i vào sum, sau đó i++ tiến tới điểm thoát.
Vòng lặp while kiểm tra điều kiện của nó...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Điều kiện được kiểm tra trước mỗi lượt, vì vậy nó có thể chạy 0 lần.
int i=1, sum=0; while (i<=5){ sum+=i; i++; } Sum ở cuối là gì?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
1+2+3+4+5 = 15.
Quên cập nhật biến điều khiển vòng lặp bên trong thân thường gây ra...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Không có sự tiến triển, điều kiện không bao giờ trở thành false.
Nếu điều kiện while là false ở lần kiểm tra đầu tiên, thân sẽ chạy 0 lần.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Kiểm tra trước mỗi lượt có nghĩa là nó có thể chạy 0 lần.
Một vòng lặp không bao giờ dừng lại vì điều kiện của nó không bao giờ trở thành false là một vòng lặp ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Một vòng lặp vô hạn không tạo ra sự tiến triển để dừng lại.