while loops · while 루프
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
while repeats
while (test) { ... }runs again and again, as long as the test staystrue.- It checks the test before each pass — so the body might run zero times.
한국어
while 반복
while (test) { ... }은 조건 테스트가true인 한 계속 다시 실행됩니다.- 각 반복 전에는 반드시 조건 테스트를 확인하므로, 본체가 0회 실행될 수 있습니다.
let n = 1;
while (n <= 3) {
console.log(n); // → 1, then 2, then 3
n++;
}
English
Make progress every pass
- Something inside must move the test toward
false. - If nothing changes, the loop never stops — an infinite loop (the Stop button saves you).
한국어
매 반복 시 진전시키기
- 내부에 무언가가 있어 조건 테스트가
false로 향하게 만들어야 합니다. - 아무것도 변경되지 않으면 루프가 멈추지 않습니다 — 무한 루프(Stop 버튼이 이를 방지해 줍니다).
let n = 1;
while (n < 50) {
n = n * 2; // 2, 4, 8, 16, 32, 64 — then stop
}
console.log(n); // → 64
Explore · 탐색하기
Each pass doubles n · 각 반복마다 n이 두 배가 됩니다
while re-checks its test each pass. n must change, or it runs forever. · while은 매 반복마다 조건을 재확인합니다. n이 변하지 않으면 무한히 실행됩니다.
English
Common mistakes
- Change the condition inside the loop, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
한국어
흔한 실수
- 루프 내부의 조건을 변경하지 않으면 영원히 실행됩니다.
while은 먼저 조건을 확인하므로 0회 실행될 수 있습니다.
English
Now you try
- Use a
whileloop. - Press Check answer to test it.
한국어
이제 직접 해보기
while루프를 사용하십시오.- 답안 확인을 눌러 테스트해 보십시오.
let n = 3. Use a while loop to count down: log 3, 2, 1, one per line. · let n = 3. while 루프를 사용하여 카운트다운: 3, 2, 1을 각각 한 줄씩 출력합니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
let n = 1. While n is 16 or less, log n then double it. Output should be 1, 2, 4, 8, 16. · let n = 1. n이 16 이하일 동안, n을 출력하고 이를 두 배로 만듭니다. 출력은 1, 2, 4, 8, 16이어야 합니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Use a while loop to add up 1 to 4, then log the total (it is 10). · while 루프를 사용하여 1부터 4까지의 합을 구한 후, 총합을 출력합니다(그 결과는 10입니다).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.