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.
조건이 참인 동안 루프
while루프는 조건이 참인 동안 반복합니다.- 각 반복 전에 조건을 확인합니다.
- 반복 횟수를 미리 알 수 없을 때 사용합니다.
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.
항상 조건 변경하기
- 루프 내부의 무언가가 조건을 거짓으로 만들도록 이동해야 합니다.
- 절대 거짓이 되지 않으면 루프가 영원히 실행됩니다 (무한 루프).
- 루프가 갇히는 경우 정지 버튼을 누르십시오.
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와 continue
break은 루프를 즉시 종료합니다.continue은 이번 반복의 나머지를 건너뛰고 다음 반복으로 갑니다.while True:은break이 루프를 끝낼 때까지 영원히 반복합니다.
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.
전후 확인하기
while루프는 먼저 확인하므로, 영번 실행될 수도 있습니다.- "until until" 루프는 뒤에서 확인하므로, 최소 한 번은 반드시 실행됩니다.
- Python에는
repeat이 없습니다;while True:에 끝자리로break을 사용합니다.
In Cambridge pseudocode
whileis the pre-condition loop;REPEAT … UNTILis the post-condition loop.
캐미지아 가위코드에서
while은 전제조건 루프이고;REPEAT … UNTIL은 후제조건 루프입니다.
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.
흔한 실수
- 루프 내부에서 무언가를 변경하여 조건이 거짓이 될 수 있게 하거나, 영원히 실행됩니다.
while은 먼저 조건을 확인하므로 0회 실행될 수 있습니다.
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.
이제 직접 해보기
- 조건이 결국 거짓이 될지 확신하십시오.
- 루프에 갇혔나요? 먼저 단축 연습 카운트다운 완성을 해보세요. 결선 라인이 하나 부족합니다.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
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. · 출력을 보려면 '실행'을 클릭하세요.
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. · 출력을 보려면 '실행'을 클릭하세요.
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. · 출력을 보려면 '실행'을 클릭하세요.
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. · 출력을 보려면 '실행'을 클릭하세요.
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. · 출력을 보려면 '실행'을 클릭하세요.