Iteration
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
Repeat with iteration
- Iteration means doing the same steps again and again.
- Loops save you from writing the same line many times.
- AP CSP cares about loops because they let a little code do a lot of work.
한국어
반복을 위한 반복문
- 반복문은 동일한 단계들을 계속 수행하는 것입니다.
- 루프는 동일한 줄을 여러 번 쓸 필요 없이 도와줍니다.
- AP CSP에서는 루프가 적은 코드로 많은 일을 하게 하므로 중요합니다.
English
Count with for and range
for i in range(n):repeats the bodyntimes.range(5)gives0, 1, 2, 3, 4(it stops before 5).- The loop body is indented, like an
if.
한국어
for와 range로 세기
for i in range(n):는 본문을n회 반복합니다.range(5)는0, 1, 2, 3, 4을 제공합니다 (5보다 앞에서 멈춥니다).- 루프 본문은
if처럼 들여쓰기되어 있습니다.
for i in range(5):
print(i)
English
Build a total (accumulation)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it each pass to build a running total.
한국어
합계 구축 (누적 계산)
- 누적 변수는 루프 내부에서 커지는 변수입니다.
- 루프 전에
0로 시작하십시오. - 매 반복마다 더하여 누적 합계를 만듭니다.
total = 0
for n in range(1, 6):
total = total + n
print(total)
English
Loop until a condition with while
- A
whileloop repeats as long as its condition isTrue. - Something inside must move toward making it
False. - Use it when you do not know the count in advance.
한국어
조건이 만족될 때까지 while로 루프
while루프는 조건이True인 동안 반복됩니다.- 내부에something이 있어 조건이 eventually
False가 되도록 해야 합니다. - 미리 횟수를 모를 때 사용합니다.
count = 3
while count > 0:
print(count)
count = count - 1
English
Loop over a list (for-each)
for item in mylist:visits each item in turn.- The loop variable holds one item per pass.
- This is the clean way to process every value.
한국어
리스트 순환 (for-each)
for item in mylist:는 순차적으로 각 항목을 방문합니다.- 루프 변수는 매 반복 시 하나의 항목을 가집니다.
- 모든 값을 처리하는 가장 깔끔한 방법입니다.
scores = [10, 20, 30]
for s in scores:
print(s)
English
In AP CSP pseudocode
- CB has three loop shapes that match Python loops.
REPEAT n TIMES= a fixed count;REPEAT UNTIL(cond)= loop until true.FOR EACH x IN list= a for-each loop.DISPLAY= print,←==,MOD=%.
한국어
AP CSP 가위수식에서
- CB에는 Python 루프와 대응되는 세 가지 루프 형태가 있습니다.
REPEAT n TIMES= 고정된 횟수;REPEAT UNTIL(cond)= 참이 될 때까지 루프.FOR EACH x IN list= for-each 루프.DISPLAY= print,←==,MOD=%.
REPEAT 5 TIMES
{
DISPLAY "hi"
}
count ← 3
REPEAT UNTIL (count = 0)
{
DISPLAY count
count ← count - 1
}
FOR EACH s IN scores
{
DISPLAY s
}
English
Common mistakes
range(n)runs0ton - 1.- Change the condition inside a
while, or it never stops.
한국어
흔한 실수
range(n)는0부터n - 1까지 실행됩니다.while내부에서 조건을 변경하지 않으면 루프가 끝나지 않습니다.
English
Now you try
- Use a loop in each task.
- Press Check answer to test your code.
한국어
이제 직접 해보기
- 각 작업에서 루프를 사용하십시오.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Explore · 탐색하기
Iteration repeats the body
A loop repeats; a counter and a total change each pass.
Use a for loop with range(...) to add the whole numbers 1 through 10. Store the result in total. (It should be 55.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Use a while loop to print the numbers 5, 4, 3, 2, 1, each on its own line, then print Go. Start from n = 5.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
nums is [3, 6, 7, 8, 10, 11]. Loop over it with a for-each loop and count how many values are even (use % 2 == 0). Store the count in evens. (It should be 3.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.