Unit 2 · Selection and Iteration · Topic 2.1Algorithms with Selection and Repetition
Skill 1.A
Big idea: Every algorithm is built from three blocks: do steps in order (sequencing), choose between paths (selection), and repeat (repetition).
Learning objectives- Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.
The three building blocks
| Block | Everyday example | Java |
|---|
| Sequencing | Put on socks, then shoes | Statements one after another |
| Selection | If it's raining, take an umbrella | if, if-else |
| Repetition | Stir until the sugar dissolves | while, for |
Order changes the outcome
The same steps in a different order can give a different result. Here, checking before or after adding changes what is printed.
Codeint total = 0;
for (int i = 1; i <= 4; i++) {
total += i;
if (total > 5) {
System.out.println("over at i = " + i);
}
}
Outputover at i = 3
over at i = 4
Key points- The building blocks are sequencing, selection, and repetition.
- Selection picks a path based on a true/false decision.
- Repetition repeats a process until a goal is reached.
- The order in which blocks are combined affects the outcome.
Watch out- Swapping two steps can change the result even when the steps themselves are the same.
Quick check"Keep asking for a password until it is correct, then say welcome." Which blocks does this use?Show answer
Repetition (keep asking), selection (is it correct?), and sequencing (then say welcome).