Iteration · 迭代(循环)
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.
用迭代来重复
- 迭代(iteration)是指一遍又一遍地做同样的步骤。
- 循环让你不用把同一行代码写很多次。
- AP CSP 重视循环,因为它们让一点点代码就能完成大量工作。
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)
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.
累加出一个总和(accumulation)
- 累加器(accumulator)是在循环里不断变大的变量。
- 在循环之前把它从
0开始。 - 每一轮往里加,累出一个运行中的总和。
total = 0
for n in range(1, 6):
total = total + n
print(total)
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就一直重复。- 循环里必须有东西朝着让条件变成
False的方向走。 - 当你事先不知道次数时使用它。
count = 3
while count > 0:
print(count)
count = count - 1
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)
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= 逐项循环。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
}
Common mistakes
range(n)runs0ton - 1.- Change the condition inside a
while, or it never stops.
常见错误
range(n)从0到n - 1。- 在
while里改动条件,否则它永不停止。
Now you try
- Use a loop in each task.
- Press Check answer to test your code.
现在轮到你
- 在每个任务里使用一个循环。
- 按检查答案来测试你的代码。
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.) · 用带 range(...) 的 for 循环把整数 1 到 10 加起来。把结果存进 total。(应为 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. · 用 while 循环打印 5, 4, 3, 2, 1,每个数字一行,然后打印 Go。从 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.) · nums 是 [3, 6, 7, 8, 10, 11]。用 for-each 循环遍历它,数一数有多少个值是偶数(用 % 2 == 0)。把个数存进 evens。(应为 3。)
Click Run to see the output here. · 点击“运行”查看此处输出。