Topic 2.1 Notes – Algorithms with Selection and Repetition
1. The Three Building Blocks of Algorithms
Everything comes back to these three patterns. Whether it’s Java code, pseudocode, or describing how to order food at a kiosk, the structure is the same.
As you read, use the diagram below as a visual reference for how each structure flows.
Sequence, selection, and iteration in flowchart form
Sequencing
Sequencing means steps happen in a specific order.
- One action follows another.
- This is the default structure of any algorithm.
- Changing the order can change the result completely.
If you brush your teeth before putting toothpaste on the brush, the outcome is different. Same steps. Wrong order.
The key idea is simple: order affects results.
Selection
Selection introduces decision-making.
- The algorithm evaluates a true/false condition.
- If the condition is true → one path.
- If false → a different path (if provided).
In pseudocode, you’ll see patterns like:
IF (condition) THEN
do something
ELSE
do something else
Selection is always based on a boolean idea. There is no “kind of true.” It either is or isn’t.
Core idea: execution branches based on a condition.
Repetition
Repetition means doing something more than once.
- It continues until a goal is reached.
- It may run a fixed number of times.
- Or it may run until something becomes true.
Pseudocode versions:
REPEAT
do something
UNTIL (condition)
or
WHILE (condition)
do something
The most important detail: something inside the loop must move toward the stopping condition.
These three patterns are the foundation of every algorithm you will write in AP Computer Science A.
2. How Selection Works in Algorithms
Selection controls how an algorithm adapts to different inputs.
Basic Logic
Every selection does two things:
- Evaluate a condition.
- Choose a path based on the result.
If there’s no ELSE, you have incomplete selection:
IF (score >= 90)
print "A"
What happens if the score is 85? Nothing. That might be intentional. Or it might be a mistake.
With complete selection, every possibility is handled:
IF (score >= 90)
print "A"
ELSE
print "Not A"
Order of Conditions Matters
In multi-branch structures:
IF (x > 0)
ELSE IF (x > 10)
The second condition might never run. Why? Because any value greater than 10 already satisfied x > 0.
Earlier conditions are checked first. Once one is true, the rest are skipped.
On multiple choice questions, this is a favorite trick. Always ask:
- Could more than one condition be true?
- Which one is checked first?
3. How Repetition Works in Algorithms
Repetition handles repeated tasks efficiently.
A correct loop has two parts:
- A stopping condition.
- A change inside the loop that moves toward stopping.
If nothing changes, the loop never ends. That’s an infinite loop.
Example of a logical error:
REPEAT
check if task is done
UNTIL task is done
If nothing inside the loop helps complete the task, it runs forever.
When Is the Condition Checked?
There’s a logical difference between:
- Checking before performing an action.
- Checking after performing an action.
If you check after, the action happens at least once.
That detail shows up in tracing questions. Sometimes the algorithm performs one extra step because the check happens at the end.
Selection Inside Repetition
Very common structure:
REPEAT
get input
IF (correct)
stop
UNTIL done
The loop repeats. The selection decides what happens during each pass. Most search-style problems follow this pattern.
4. Combining Sequencing, Selection, and Repetition
Real algorithms mix all three.
Selection Inside Repetition
For each item:
- Check a condition.
- Act based on that condition.
This is how you process lists, grades, or game turns.
Repetition Controlled by Selection
The loop continues because a condition remains true. The decision controls whether repetition keeps happening.
Sequencing Around Control Structures
Some steps must happen once before a loop starts. Some must happen after it ends.
Putting setup steps inside a loop by accident changes behavior. That’s a common logic mistake.
The big idea here is structural:
The order in which you combine these patterns determines the outcome.
Same pieces. Different arrangement. Different result.
5. Common Logic Errors to Watch For
- Infinite loops
The condition never becomes false because nothing changes. - Missing cases in selection
Some inputs are not handled at all. - Overlapping conditions in wrong order
Earlier conditions block later ones. - Wrong placement of steps
Setup code inside a loop.
Checking a condition too late.
When you see an algorithm on a quiz or the AP exam, mentally label:
- Where is the sequencing?
- Where is the decision?
- What repeats?
- Does it logically stop?