2.01 Algorithms with Selection and Repetition
Describe everyday algorithms with steps, decisions, and repeats using words, flowcharts, and pseudocode.
- 1. LxD Cycle Process
- 2. Lesson Plan
- 3. Reference Guide
- 4. Code Examples
- A. Simple: From Steps to Java
- Code Runner Challenge
- 5. Hacks & Practice Tasks
- 6. Grading Plan (1 Point Total)
- 7. Lesson Revisions & Feedback Evidence
- 8. Summary
- 9. References
1. LxD Cycle Process
Empathize: Students can follow a program line by line but stall when asked to design one, because they treat an algorithm as something that only exists in code. They do not connect it to the ordered steps they already follow every day.
Define:
- POV: CSA students need to recognise sequence, selection and repetition in plain language first, because a student who cannot name the block in English will not choose the right Java keyword for it.
- Learning Goal: Students will describe an everyday process as an algorithm, identify which steps are selection and which are repetition, and express the same algorithm in written steps, pseudocode and a flowchart.
Ideate:
- HMW Question: How might we get students to see the control structures they already use every day, before we attach Java syntax to them?
- Activity: Walk a vending machine flowchart, sort steps into sequence, selection and repetition, then reorder a shuffled algorithm until it runs correctly.
Prototype & Test: The lesson opens with a flowchart walk rather than a definition, so the three blocks are named only after students have already traced them. The Java runner comes last, once the algorithm is already understood on paper.
2. 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 (5 minutes)
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.
3. 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).
4. 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.
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);
5. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one markdown cell at the top with the frontmatter:
---
layout: post
title: CSA Unit 2.01 Algorithms with Selection and Repetition HW
categories: [Java, Algorithms-with-Selection-and-Repetition]
lesson_language: Java
lesson_topic: Algorithms-with-Selection-and-Repetition HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_1_hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Ensure all code runs and output is visible.
- Include a markdown cell before each code section explaining the concept.
Submission Safety Rules (Read First)
[IMPORTANT] To avoid grading errors, follow these rules exactly:
- Write your prediction before you run each code section
- Execute every code cell and leave the output visible
- Show your work for any hand-traced prediction
- Do not just copy code; explain what each line does
- Label each question clearly
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 (In-Class)
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.
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.
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?
6. Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Student predicted the number of
Insertedlines and the change amount before running, then verified against the output. -
0.8 points: Homework completion
- 0.4 Part 1 algorithm: A written algorithm of at least six steps that contains one selection and one repetition, in the pseudocode style used in the lesson table.
- 0.3 Part 2 Java: The same algorithm implemented in the runner with a
whileloop for the repetition and aniffor the selection, printing a line per step. - 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.
Quick Validation Checklist
- Present: prediction written before the Popcorn runner was executed
- Present: six or more numbered steps with one selection and one repetition
- Present: Java version whose printed output matches the written steps
- Test: changing
priceorcoinchanges the number ofInsertedlines
7. Lesson Revisions & Feedback Evidence
Feedback Received: Team review of the Unit 2 set found that these lessons did not follow the section format used by the Unit 1 lessons, and that they ended without the assignment submission form every other CSA lesson provides.
Revision Made: The lesson was reorganised into the shared CSA lesson format, so it now opens with the LxD cycle and lesson plan, states its reference material before the code, and groups the activities, Popcorn Hack and Homework Hack under one practice section. The duplicate lesson heading was removed, and the page now renders the standard Submit Assignment form at the end.
Design Decision Kept: The lesson opens with a flowchart walk rather than a definition, so the three blocks are named only after students have already traced them. The Java runner comes last, once the algorithm is already understood on paper.
Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.
8. Summary
- Algorithms are built from sequence, selection, and repetition.
- Selection asks a yes or no question and picks a path:
if. - Repetition repeats steps while a condition stays true:
while,for. - Plain language, flowcharts, and pseudocode describe the same algorithm; Java is just a precise way to write it.
- Next lesson: the yes or no questions themselves, called Boolean expressions.
9. 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 objective quoted in section 2 of this lesson 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/
Submit Assignment
Need to update a submission later? Open the submissions dashboard.