Selection and iteration
導入済み
23:18
プログラミング基礎
プログラムはパスワードを求め、正しい値が入力されるまで繰り返し要求する必要があります。単純ですが、どのループを使いますか? プログラムは判断を行い、作業を繰り返しますが、選択を…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| security update | A change to software that fixes weaknesses attackers could use. |
| code/kəʊd/ | Instructions written in a programming language for a computer to follow. |
| certificate/səˈtɪfɪkət/ | A digital document that helps prove the identity of a website. |
| attacker | A person who tries to harm or gain unauthorised access to systems or data. |
| selection/sɪˈlekʃn/ | 選択 |
| Iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| constructs | Basic ways to organise steps in an algorithm, including order, choice and repetition. |
| trace table/treɪs ˈteɪbl/ | トレーステーブル |
| condition/kənˈdɪʃn/ | A requirement or circumstance that affects what happens or whether something is true. |
| Boolean expression/ˈbuːlɪən ekˈspreʃn/ | An expression whose value is true or false. |
| comparison/kəmˈpærɪsn/ | An operation that compares two values and gives a true or false result. |
| branch/bræntʃ/ | A path of instructions chosen according to a condition or value. |
| nested/ˈnestɪd/ | ネスト |
| logic error/ˈlɒdʒɪk ˈerə/ | ロジックエラー |
| boundary value | A value at the edge between two possible outcomes of a condition. |
| guard/ɡɑːd/ | A value, list or range that determines when a CASE branch runs. |
| assignment/əˈsaɪnmənt/ | Storing a value in a variable or other named location. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| block/blɒk/ | A group of instructions treated as a unit in a program. |
| count-controlled loop/kaʊnt kənˈtrəʊld luːp/ | カウント制御ループ |
| element/ˈelɪmənt/ | A substance made of atoms with the same proton number. |
| array/əˈreɪ/ | 配列 |
| pre-condition loop/priː kənˈdɪʃn luːp/ | 前条件ループ |
| post-condition loop/pəʊst kənˈdɪʃn luːp/ | 後条件ループ |
| loop body | The instructions inside a loop that are repeated when the loop runs. |
| counter/ˈkaʊntə/ | A device or variable that records how many events or items have occurred. |
| dry run/draɪ rʌn/ | ドライラン |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| keyword/ˈkiːwɜːd/ | A word with a fixed meaning in a programming language or pseudocode. |
余分な1行
- 2014のAppleは単一の重複行のためにセキュリティアップデートを出した。ウェブサイト証明書を確認するコードの深部で、
goto fail;が連続して出現していた。 - 2番目のコピーは
IFの外側にあったため、毎回実行され、その下のチェックは一度も実行されなかった。数ヶ月間、攻撃者は任意のウェブサイトを装うことができた。 - 選択と反復は、何を実行し、いく度実行するかを決める構文である。これらの中身が1行間違ってもプログラムは動作するが、誤った結果になるだけである。
- この授業はIF、CASE、および3つのループ、そして試験前に間違いを見逃さないトレース表に関するものである。

選択
IF age >= 18 THEN … ELSE … ENDIF
ELSEは任意であるが、ENDIFは必須である。すべてのIFは閉じなければならない。- 条件はあらゆるブール式である:比較、または
AND、OR、NOTで結合された比較。

IF … ELSE は一度テストし、その後ちょうど1つの分支を実行する

嵌套IFと境界値
- 嵌套IFとは、別のIFのbranch内にあるIFであり、それぞれが独自の
ENDIFを必要とする。 - 境界値で失点します。「50点以上で合格」は
Mark >= 50であり、Mark > 50ではありません。 - 不適切な比較は 論理エラー です:プログラムは実行され、ある入力的に誤った答えを返します。境界値そのものである 50 を用いたトレースが、それを発見する方法です。

"A mark of 50 or more passes" is written as IF Mark ____ 50 THEN.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
"Or more" includes 50 itself, so the comparison is greater than or equal. > would fail a student on exactly 50.
ケース
CASE OF Grade … ENDCASEは 1つの値 を複数の選択肢と照合し、ネストした IF の連鎖よりもすっきりしています。- ガードは単一値
"A":、リスト1, 2, 3:、または範囲1 TO 5:になれます。「それ以外」のための最終分岐はOTHERWISEであり、> 200のような条件ではありません。 - ガードは 順序通り にテストされます。
1 TO 50:に40 TO 60:が続き、値が 45 の場合、最初の分岐を取りますので、2つ目の分岐内の代入文は実行されない可能性があります。初期の分岐で可能な値をすべて網羅している場合、OTHERWISEも実行されません。

A CASE文は、値に一致する最初の分岐を実行します

A CASE statement is cleaner than nested IFs when you are:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
CASE matches one value against many possibilities; deep nested IFs become hard to read.
Which of these are valid guards in a Cambridge CASE statement? Select すべて that apply.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
A single value, a value list, a range and OTHERWISE. A comparison such as > 200 is not a guard; anything not covered goes to OTHERWISE.
演習例: CASE を使わずに再記述する
CASE OF MySwitchを1: ThisChar ← 'a',2: ThisChar ← 'y',3: ThisChar ← '7',OTHERWISE: ThisChar ← '*'と使用します。- 各値がIFの連鎖の分岐となり、
OTHERWISEが最終的なELSEとなります。
IF MySwitch = 1 THEN
ThisChar <- 'a'
ELSE
IF MySwitch = 2 THEN
ThisChar <- 'y'
ELSE
IF MySwitch = 3 THEN
ThisChar <- '7'
ELSE
ThisChar <- '*'
ENDIF
ENDIF
ENDIF
- 3つのIFと3つのENDIF。逆に見ると、同じ値を代入する2つのCASE節は、値リストを持つ1つのガードに統合され、
1, 2:となります。
3つのループ
- 反復 はブロックを繰り返すことで、3つのループは条件のテスト位置が異なります。
- カウント制御ループ
FOR i ← 1 TO 10 … NEXT iは既知の回数だけ実行され、STEP -1はカウントダウンします。固定された回数を指定する場合や、配列 のすべての要素を処理する場合に使用します。 - 前条件ループ
WHILE … DO … ENDWHILEは各反復の前にテストを行うため、ゼロ回しか実行されないことがある。後条件ループREPEAT … UNTILは各反復の後にテストを行うため、少なくとも一度は実行される。

条件の位置が、本体が実行される回数を決定します

Match each loop to when you'd use it.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
FOR = count-controlled; WHILE = condition tested before (0+ passes); REPEAT = condition tested after (1+ passes).
REPEAT...UNTIL tests its condition AFTER the body, so the body always runs at least once.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
That post-condition test is the difference from WHILE, which can run zero times.
ループの選択:2点満点の解答
- 名前 に1点、理由 に1点、評価基準の言葉を用いて答えます。
- 反復回数がループ開始前に既知であるため、カウント制御型である。 ループ本体が少なくとも一度は実行される必要があるため、後条件型である。 ループが全く実行されない可能性もあるため、前条件型である。
- カウンター付きの
WHILEで配列の4要素をループすることは「最も適切ではない」とされます。FORループが適切なのは、回数が既知だからです。
A question asks you to "identify a more appropriate loop and justify your choice" for 2 marks. What earns the two marks?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
One mark for the loop, one for the reason: the count is known, the body must run once, or the loop may not run at all.
このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
Worked example: which loop fits each task?
- Print the 12 times table: the count is known in advance, so a FOR loop 循环.
- Keep reading numbers until the user enters 0: at least one number must be read before it can be tested, so a REPEAT … UNTIL loop is suitable.
- Ask for a password until it is correct: the user must be asked at least once, so a REPEAT … UNTIL loop.
- Ask two questions: how many times, and must the body run before the first test?

"Keep asking for a password until it is correct, but always ask at least once." Which loop fits?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
You must ask at least once, so the post-condition REPEAT...UNTIL is the natural choice.
トレース表
- トレース表 はアルゴリズムを手動で ドライラン する際、すべての変数の値を記録します。これは紙上でループをテストする方法であり、多くのPaper 2における6点問題です。
- 変数ごとに1列、出力がある場合は別列。値が変わったときに新しい行を書き込み、ループが条件に到達した瞬間にループ条件を評価する。
- 条件がループを終了するまで続け、最後に出力を記録します。
Trace a loop, pass by pass
A trace table records each variable after every pass of the loop. Watch the counter i climb while the running total builds up — exactly what an exam trace question asks you to fill in.
後 total = 0; FOR i = 1 TO 5: total = total + i, what is the value of total?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Adding 1+2+3+4+5 = 15 — exactly what the trace table builds up pass by pass.
演習例:埋められたトレース表
Count <- 1
Total <- 0
WHILE Total < 10 DO
Total <- Total + Count
Count <- Count + 1
ENDWHILE
OUTPUT Count
- (Count, Total) の行:(1, 0) → (2, 1) → (3, 3) → (4, 6) → (5, 10)。
- Total = 10 のとき、条件
Total < 10は false となり、ループは終了し、出力は 5 となります。 - 最後の行が最も間違えやすいです:出力を書く前に条件をもう一度確認してください。

In the worked trace, the loop ends with Total = 10 and Count = 5, so the output is 5.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
After the fourth pass Total reaches 10, the condition Total < 10 becomes false, and Count has been incremented to 5.
見落としやすいポイント
>=と>は異なる解答です。比較を書く前に頭の中で境界値をテストしてください。OTHERWISEは条件ではなくキーワードです。また、IF,CASE,WHILE,FORのすべてはENDIF,ENDCASE,ENDWHILE,NEXTで終わります。WHILEは0回実行できるのに対し、REPEATはできません。最初のテスト前に本体が1回実行される必要があるかどうかで選択します。- トレース表において、値が変わらないものは書き換えられず、出力は
OUTPUTの行が実行されたときにのみ書かれます。
理解できましたか
- 選択には IF … ELSE … ENDIF、ネストしたIFそれぞれにENDIF、多くのガードに対する1つの値には CASE(順序通りテストされ、
OTHERWISEが最後)。 - FOR: カウントが既知 · WHILE: 前にテスト、ゼロ回実行あり得る · REPEAT … UNTIL: 後にテスト、少なくとも一度実行
- ループをその名前と理由で正当化せよ: 既知のカウント、実行されない可能性、必ず一度実行される
- トレース表 はアルゴリズムをドライランし、変化ごとに1行、最終出力前に条件をもう一度確認
何が問題でしたか?この内容はサイトの運営チームだけが確認します。