Циклы while
Введено| English | Значение |
|---|---|
| while loop/waɪl luːp/ | Цикл while |
| loop control variable/luːp kənˈtrəʊl ˈveərɪəbl/ | Переменная управления циклом |
| infinite loop/ˈɪnfɪnət luːp/ | Бесконечный цикл |
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.
Суммирование от 1 до 5 с помощью while
Каждая итерация добавляет i к sum, затем i++ приближает выход.
Цикл while проверяет свое условие...
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Условие тестируется перед каждой итерацией, поэтому тело может выполниться 0 раз.
int i=1, sum=0; while (i<=5){ sum+=i; i++; } Чему равно sum в конце?
Сложность 2/5 · 140 монет + небольшой бонус за первое выполнение сегодня
1+2+3+4+5 = 15.
Забывание обновить переменную управления циклом внутри тела обычно приводит к...
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Без прогресса условие никогда не станет ложным.
Если условие while ложно при первой проверке, тело выполнится ноль раз.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Проверка перед каждой итерацией означает, что оно может выполниться 0 раз.
Цикл, который никогда не останавливается, потому что его условие никогда не становится ложным, называется ___ циклом (одно слово).
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Бесконечный цикл не делает прогресса toward stopping.