UNIT 2: SELECTION AND ITERATION · TOPIC 2.1
2.1 Algorithms with Selection and Repetition
Every algorithm is sequence, selection, and repetition. This topic names them and shows how they combine.
What you need to know
- Sequence: statements executed in order. Selection: a decision that chooses between paths (
if). Repetition (iteration): a loop that repeats code (while,for). - An algorithm can be described in words, in a flowchart, or in code. The same logic can be implemented several equivalent ways.
- Reading an algorithm: identify the initializations, what decisions are made, what repeats, and what's true when it stops.
- Tracing is the essential skill — a table with one column per variable and one row per executed statement or loop pass.
- Two algorithms are equivalent if they produce the same result for all inputs, even if the code differs.
Worked example
int total = 0; // sequence
for (int i = 1; i <= 5; i++) // repetition
{
if (i % 2 == 1) // selection
{
total += i;
}
}
// total is 1 + 3 + 5 = 9
Exam tip: Before tracing, write what each variable starts as. Then execute statements strictly in order. If a question describes an algorithm in English and asks which code implements it, translate the English into initializations, a loop, and a decision — then match.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- The three control structures are complete: any algorithm can be expressed with only sequence, selection, and repetition. There's no fourth thing to learn — methods and recursion are ways of organizing these three.
- Reading an algorithm from code: find what's initialized before the loop (the accumulator or tracker), what the loop iterates over, what the selection decides each time, and what's true when the loop ends. Those four facts identify the algorithm.
- Writing an algorithm from a description: the description's nouns are variables, its "for each" or "until" is the loop, its "if" is the selection, its "total/count/largest" is the accumulator's role.
- Two code segments are equivalent when every input produces the same result — not when they look similar. The exam's equivalence questions are best answered by testing with specific inputs, including boundaries.
- Tracing discipline: a table with a column per variable, a row per statement executed (or per loop iteration). Write the values, not the expressions. This is a skill to practice, not a concept to understand.
Mistakes that cost points
- Tracing mentally. Write it down. Every time.
- Identifying an algorithm by one variable name. A variable named
summight be counting. Read what's added to it. - Checking equivalence with one input. One match proves nothing. Try a boundary and a normal case.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which control structure is demonstrated by the code segment
if (x > 0) { count++; }?Show answer
Answer: B. An if statement chooses whether to execute code — that's selection.
Q2 An algorithm should add up the even numbers from 2 to 10. Which of the following correctly does so?
Show answer
Answer: A. Start at 2, step by 2, include 10. Option B stops at 8; C adds all numbers; D starts wrong and adds odds.
Key vocabulary
- Sequence
- statements executed one after another
- Selection
- choosing a path with a conditional
- Repetition
- repeating with a loop; also called iteration