while Loops · while 循环
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| while loop/waɪl luːp/ | 当型循环 | dāng xíng xún huán |
| loop control variable/luːp kənˈtrəʊl ˈveərɪəbl/ | 循环控制变量 | xún huán kòng zhì biàn liàng |
| infinite loop/ˈɪnfɪnət luːp/ | 无限循环 | wú xiàn xún huá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.
只要为真就重复
- while 循环****只要一个条件保持
true就重复它的主体。 while (i <= 5) { sum = sum + i; i++; }- 在每一遍之前,Java 都检查条件;若为
false,循环结束。 - 当你事先不知道需要多少遍时用
while。
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.
每遍之前先检验
- 每次主体运行之前都检验条件。
- 如果第一次检查它就为
false,主体运行零次。 - 每遍之后,在下一遍之前再次检查条件。
- 这种“先检查,再或许运行”的顺序值得记住。
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.
循环控制变量
- 循环控制变量推动朝停止的进展。
- 在循环前初始化它,在条件里使用它,并在主体内部改变它。
i从1开始,条件检查i <= 5,而i++让它前进。- 没有一个最终打破条件的改变,循环永不结束。
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.
无限循环
- 无限循环发生在条件永远不变为假时。
- 忘了
i++(或更新了错误的变量)会把程序永远困住。 - 判断标志:循环没有朝它的停止条件取得任何进展。
- 总要确保每一遍都把控制变量朝出口移动。
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.
while 在每遍之前检验条件——所以它可能运行零次。如果条件已经为 false,主体永不运行。而且你必须在主体内部改变循环控制变量,使条件最终变为 false;忘了更新(例如没有 i++)是经典的无限循环。
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.
对 1 到 5 求和:
int i = 1, sum = 0;while (i <= 5) { sum = sum + i; i++; }- 各遍:
i=1sum=1,i=2sum=3,i=3sum=6,i=4sum=10,i=5sum=15;然后i=6不满足<= 5→ 停止。sum是15。
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.
while 循环在条件为 true 时重复它的主体,在每遍之前检查(所以它可能运行零次)。循环控制变量必须被初始化、检验,并在主体内部更新,让循环取得进展——忘了更新会造成无限循环。
Summing 1 to 5 with while · 用 while 对 1 到 5 求和
Each pass adds i to sum, then i++ moves toward the exit. · 每遍把 i 加进 sum,然后 i++ 朝出口移动。
A while loop checks its condition... · while 循环检查它的条件是在……
The condition is tested before each pass, so it can run 0 times. · 条件在每遍之前检验,所以可能运行 0 次。
int i=1, sum=0; while (i<=5){ sum+=i; i++; } What is sum at the end? · int i=1, sum=0; while (i<=5){ sum+=i; i++; } 结束时 sum 是多少?
1+2+3+4+5 = 15. · 1+2+3+4+5 = 15。
Forgetting to update the loop control variable inside the body usually causes... · 忘了在主体内部更新循环控制变量通常会造成……
With no progress, the condition never becomes false. · 没有进展,条件永不变为假。
If a while condition is false on the first check, the body runs zero times. · 如果 while 条件在第一次检查时为假,主体运行零次。
Test-before-each-pass means it can run 0 times. · 每遍之前先检验意味着它可能运行 0 次。
A loop that never stops because its condition never becomes false is an ___ loop (one word). · 一个因条件永不变假而永不停止的循环是 ___ 循环(填英文一词 infinite)。
An infinite loop makes no progress toward stopping. · 无限循环不朝停止取得进展。