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.
イテレーションで繰り返す
- イテレーションとは、同じステップを繰り返し行うことです。
- ループを使うと、同じ行を何度も書く必要ななくなります。
- 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.
合計を構築する(蓄積)
- アクミュレーターはループ内で成長する変数です。
- ループ开始前に
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:は順に各項目を訪問します。- ループ変数は各パスで1つの項目を保持します。
- 全値を処理するための清らかな方法です。
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のループに対応する3つのループ形状があります。
REPEAT n TIMESは固定カウント;REPEAT UNTIL(cond)はtrueになるまでループします。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
}
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 · 反復(Iteration)は本体を繰り返します。
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.) · for ループと range(...) を使って、整数 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. · 実行ボタンをクリックして出力を確認してください。