2.01 Algorithms with Selection and Repetition
Describe everyday algorithms with steps, decisions, and repeats using words, flowcharts, and pseudocode.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Code Examples
- A. Simple: From Steps to Java
- Code Runner Challenge
- Hacks & Practice Tasks
- Activity 1: Walk the Flowchart
- Activity 2: Sequence, Selection, or Repetition?
- Activity 3: Put the Vending Machine in Order
- Popcorn Hack
- Code Runner Challenge
- Homework Hack
- Code Runner Challenge
- Quick Check
- References
Reference Guide
Key Ideas
- An algorithm is a step by step process that completes a task or solves a problem.
- Sequence: steps run in order, one after another.
- Selection: the algorithm makes a choice. “If the light is red, wait.”
- Repetition (iteration): steps repeat while a condition holds. “Keep stirring until the sugar dissolves.”
- Every Java program in this unit is built from these three building blocks.
- The same algorithm can be written three ways: plain language, a flowchart, or pseudocode.
One Algorithm, Three Ways
| Written steps | Pseudocode | Flowchart |
|---|---|---|
| 1. Insert a coin. 2. If the total is still less than the price, go back to step 1. 3. Press the snack button. 4. If there is change, take it. |
REPEAT UNTIL total >= priceinsert coinpress snack buttonIF total > pricetake change |
Boxes for actions, diamonds for decisions, arrows for the path. Try one below. |
- Repetition shows up as an arrow that loops back (or
REPEAT,WHILE). - Selection shows up as a diamond with a Yes path and a No path (or
IF).
College Board Requirements
AP CSA Unit 2, Topic 2.1: Algorithms with Selection and Repetition.
This lesson is built directly against the College Board’s stated learning objective for this topic:
Represent patterns and algorithms found in everyday life using written language or diagrams.
Topic 2.1, Algorithms with Selection and Repetition, is required content on the AP Computer Science A Exam. The learning objective quoted above is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Lesson Plan
Learning Objective: Represent patterns and algorithms found in everyday life using written language or diagrams.
Success Criteria: You can describe an everyday task as ordered steps, point to the step that makes a decision, point to the step that repeats, and write the same algorithm as pseudocode and as a flowchart.
Tech Talk & Introduction
Every program in this unit is built out of three blocks: sequence, selection and repetition. Before writing any Java, it helps to see those three blocks in something ordinary, like a vending machine. Once you can name the block, the Java keyword that implements it is the easy part.
- Sequence is one step after another.
- Selection is a fork in the path, written in Java as
if. - Repetition is a step that repeats while something stays true, written as
whileorfor.
Code Examples
A. Simple: From Steps to Java
The vending machine algorithm above becomes a Java program with a while loop (repetition) and an if statement (selection). Run it, then change price or coin and run again.
Vending machine: repetition with while, selection with if. Change price or coin and run again.
public class VendingMachine {
public static void main(String[] args) {
int price = 125; // price in cents
int total = 0;
int coin = 25;
// Repetition: keep inserting quarters until we have enough
while (total < price) {
total = total + coin;
System.out.println("Inserted " + coin + " cents. Total: " + total);
}
// Selection: only give change when we paid too much
if (total > price) {
System.out.println("Change: " + (total - price) + " cents");
} else {
System.out.println("Exact amount. No change.");
}
System.out.println("Enjoy your snack!");
}
}
Code Runner Challenge
Vending machine: repetition with while, selection with if. Change price or coin and run again.
View IPYNB Source
// CODE_RUNNER: Vending machine: repetition with while, selection with if. Change price or coin and run again.
public class VendingMachine {
public static void main(String[] args) {
int price = 125; // price in cents
int total = 0;
int coin = 25;
// Repetition: keep inserting quarters until we have enough
while (total < price) {
total = total + coin;
System.out.println("Inserted " + coin + " cents. Total: " + total);
}
// Selection: only give change when we paid too much
if (total > price) {
System.out.println("Change: " + (total - price) + " cents");
} else {
System.out.println("Exact amount. No change.");
}
System.out.println("Enjoy your snack!");
}
}
VendingMachine.main(null);
Hacks & Practice Tasks
Activity 1: Walk the Flowchart
Every diamond is a selection. The snooze arrow that points back up is a repetition. Answer each decision and watch the path you take.
Activity 2: Sequence, Selection, or Repetition?
Sort each step of a bread recipe and each line of Java into the building block it uses.
Click an item, then click a bucket. Or drag it. Click an item inside a bucket to send it back.
Activity 3: Put the Vending Machine in Order
An algorithm only works when the steps are in the right order. Drag the steps (or use the arrows) so the snack comes out.
Popcorn Hack
Before you run: with price = 130 and coin = 10, how many “Inserted” lines will print, and how much change is given? Write your guess, then run and check.
Popcorn hack: predict the number of coins and the change before you press Run.
public class PopcornMachine {
public static void main(String[] args) {
int price = 130;
int total = 0;
int coin = 10;
int inserted = 0;
while (total < price) {
total = total + coin;
inserted++;
System.out.println("Inserted " + coin + " cents. Total: " + total);
}
System.out.println("Coins inserted: " + inserted);
if (total > price) {
System.out.println("Change: " + (total - price) + " cents");
} else {
System.out.println("Exact amount. No change.");
}
}
}
Code Runner Challenge
Popcorn hack: predict the number of coins and the change before you press Run.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: predict the number of coins and the change before you press Run.
public class PopcornMachine {
public static void main(String[] args) {
int price = 130;
int total = 0;
int coin = 10;
int inserted = 0;
while (total < price) {
total = total + coin;
inserted++;
System.out.println("Inserted " + coin + " cents. Total: " + total);
}
System.out.println("Coins inserted: " + inserted);
if (total > price) {
System.out.println("Change: " + (total - price) + " cents");
} else {
System.out.println("Exact amount. No change.");
}
}
}
PopcornMachine.main(null);
Homework Hack
Keep it short. Two parts.
Part 1. In the text cell below, write an everyday algorithm with at least 6 steps. It must include one selection (an if) and one repetition (a loop). Use the pseudocode style from the table above.
Algorithm: ______________________
1.
2.
...
Part 2. Turn your algorithm into Java in the runner below. Use a while loop for the repetition and an if for the selection. Print a line for each step so the output tells the story.
Homework: code your own algorithm with one while loop and one if statement.
public class MyAlgorithm {
public static void main(String[] args) {
// Example idea: laps around a track. Replace it with your own algorithm.
int lapsDone = 0;
int lapsNeeded = 3;
// TODO: repetition. Loop until lapsDone reaches lapsNeeded.
while (lapsDone < lapsNeeded) {
lapsDone++;
System.out.println("Finished lap " + lapsDone);
}
// TODO: selection. Print a different message depending on a condition.
if (lapsDone >= 3) {
System.out.println("Great workout!");
} else {
System.out.println("Keep going next time.");
}
}
}
Code Runner Challenge
Homework: code your own algorithm with one while loop and one if statement.
View IPYNB Source
// CODE_RUNNER: Homework: code your own algorithm with one while loop and one if statement.
public class MyAlgorithm {
public static void main(String[] args) {
// Example idea: laps around a track. Replace it with your own algorithm.
int lapsDone = 0;
int lapsNeeded = 3;
// TODO: repetition. Loop until lapsDone reaches lapsNeeded.
while (lapsDone < lapsNeeded) {
lapsDone++;
System.out.println("Finished lap " + lapsDone);
}
// TODO: selection. Print a different message depending on a condition.
if (lapsDone >= 3) {
System.out.println("Great workout!");
} else {
System.out.println("Keep going next time.");
}
}
}
MyAlgorithm.main(null);
Quick Check
With price = 130 and coin = 10, which building block decides whether the line Change: ... prints?
What would happen if the line total = total + coin; were deleted from the loop?
References
College Board Course and Exam Description
Topic 2.1, Algorithms with Selection and Repetition, is required content on the AP Computer Science A Exam. The learning objective quoted in section 3 (College Board Requirements) is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Outside Academic Reference
Sequence, selection and iteration are the three control structures that structured programming is built on. Any algorithm can be expressed with them alone, which is why the AP course introduces them together before any single Java keyword.
Reference List
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/