TLDR
Every algorithm you write is built from three patterns: sequencing (doing steps in order), selection (choosing a path based on a true or false condition), and repetition (repeating a process until a goal is reached). In AP Computer Science A, this topic is about recognizing these patterns in everyday tasks and representing them with plain language or diagrams before you turn them into Java code.

How Do Selection and Repetition Fit Into Algorithms?
Selection adds a true-or-false decision to an algorithm, while repetition repeats steps until a goal or stopping condition is reached. In AP CSA, you use these ideas before and during coding: first to describe an algorithm in words or diagrams, then to translate the logic into if statements and loops.
Why This Matters for the AP Computer Science A Exam
Selection and repetition are the foundation of every control structure you will use for the rest of the course, including if statements and loops. Getting comfortable with these patterns now makes it much easier to read and trace code in the multiple-choice section and to plan your logic before writing free-response code. When you can describe what an algorithm does step by step, you can spot where a decision or a loop belongs and predict how the order of steps changes the result.
Key Takeaways
- The building blocks of every algorithm are sequencing, selection, and repetition.
- Selection means execution proceeds based on a true or false decision, so the algorithm follows one path or another.
- Repetition means a process repeats until a desired outcome is reached, so you need a clear ending condition.
- The order in which you combine these building blocks changes the outcome of the algorithm.
- You can represent these patterns with written steps or diagrams before writing any Java syntax.
- Patterns often combine: selection can appear inside repetition, and repetition can appear inside selection.
Core Concepts
The Three Building Blocks
Every algorithm, no matter how complex, comes from three patterns.
- Sequencing: executing steps in a specific order, one after another. This is the default mode where one action follows the next.
- Selection: making a decision based on a condition. The algorithm checks whether something is true or false and chooses which path to follow.
- Repetition: performing a process multiple times until a desired outcome is reached, instead of writing the same steps over and over.
Selection: Making Decisions
Selection happens when a choice of how the algorithm proceeds is based on a true or false decision. When the algorithm reaches a selection point, it evaluates a condition and follows one path if the condition is true and another if it is false.
Everyday examples make this clear. A vending machine chooses which item to dispense based on the button you press. You decide what to wear based on the weather. These follow an "if this, then that" structure. The decision is binary: the condition either is or is not met, which keeps selection predictable.
Repetition: Doing Things Multiple Times
Repetition means a process repeats itself until a desired outcome is reached. That might be a set number of times or until some condition is satisfied.
Think about washing a stack of dishes. You repeat the wash-rinse-dry process for each dish until all the dishes are clean. The "until all dishes are clean" part is the ending condition, and it matters. If a repetition has no way to reach its ending condition, it never stops, which is an infinite loop. Repetition keeps algorithms flexible: you write the process once and reuse it for many items.
Combining the Patterns
Real algorithms rarely use one pattern alone. You might check the weather repeatedly every few minutes (repetition holding selection), or go through a class list and flag each student with a grade above 90 (repetition with selection inside).
The order in which you combine the patterns has a big effect on the result. Checking whether you are done before or after the main step can change the outcome entirely. That is why planning the order of steps is part of designing an algorithm.
How to Use This on the AP Computer Science A Exam
Code Tracing
You will trace algorithms that combine these patterns, especially in the multiple-choice section. Practice predicting how selection and repetition interact and how reordering steps changes the output. Track the value of any condition step by step so you can tell which branch runs and how many times a repeated step executes.
Problem Solving
Before writing free-response code, identify the pattern you need. Ask yourself: Does this require a decision (selection)? Does it process multiple items (repetition)? Does the order of steps matter (sequencing)? Sketching the algorithm in plain steps first helps you organize a solution and avoid logic errors before you commit to Java syntax.
Representing Algorithms
This topic focuses on representing patterns using written language or diagrams. Get comfortable describing an everyday task as ordered steps with clear decision points and loops. That habit translates directly into reading and writing the control structures coming up later in this unit.
Worked Examples
These examples use pseudocode-style comments to show the patterns before you translate them into Java.
</>Java// Example: Morning routine algorithm (pseudocode style) public class MorningRoutine { public static void main(String[] args) { // SEQUENCING - steps in order // 1. Wake up // 2. Turn off alarm // 3. Get out of bed // SELECTION - decision based on condition // IF (weekday) THEN // Get dressed for work // ELSE // Stay in pajamas // REPETITION - repeat until condition met // WHILE (not fully awake) // Drink coffee // Wait 5 minutes // COMBINING PATTERNS // REPEAT for each piece of clothing needed // IF (clean clothes available) THEN // Put on clothes // ELSE // Do laundry first // This might contain more repetition! } }
</>Java// Example: Library book search algorithm public class LibrarySearch { public static void explainBookSearch() { // SEQUENCING - Initial steps // 1. Go to correct section (by subject) // 2. Find correct shelf (by call number range) // REPETITION with SELECTION inside // 3. REPEAT for each book on shelf: // Look at book spine // IF (this is the book) THEN // Take book and stop searching // ELSE // Move to next book // UNTIL (found book OR reached end of shelf) // SELECTION after repetition // 4. IF (book not found) THEN // Ask librarian for help } }
Practice Problems
Problem 1: Identify the patterns in this algorithm for making a sandwich.
- Get two slices of bread
- IF (want peanut butter) THEN add peanut butter
- IF (want jelly) THEN add jelly
- REPEAT for each additional topping wanted: add topping to sandwich
- Put slices together
Solution:
- Sequencing: Steps 1 and 5 must happen in order
- Selection: Steps 2 and 3 are decisions based on preferences
- Repetition: Step 4 loops for multiple toppings
- Combination: selection happens after the opening sequence, and the repetition can contain its own implied decisions
Problem 2: Design an algorithm for a simple guessing game where the computer picks a number and the player guesses until correct.
Solution:
</>Code1. Computer picks a number (SEQUENCING) 2. REPEAT (REPETITION) Get guess from player IF (guess == number) THEN (SELECTION) Say "Correct!" and end game ELSE IF (guess < number) THEN Say "Too low" ELSE Say "Too high" UNTIL player guesses correctly
Problem 3: What is wrong with this algorithm for filling a swimming pool?
</>CodeREPEAT Turn on water Wait 1 hour UNTIL pool looks full Turn off water
Solution: The water gets turned on again every time the loop repeats. "Turn on water" should happen once, before the loop:
</>CodeTurn on water // Do this once REPEAT Wait 1 hour Check pool level UNTIL pool is full Turn off water
Common Misconceptions
- Order does not matter. It does. Putting on shoes before socks, or checking a condition at the wrong time, can change the result completely. The order of sequencing, selection, and repetition affects the outcome.
- Selection can have a "maybe" outcome. Selection is based on a true or false decision. The condition either is or is not met, and the algorithm follows exactly one path from that decision.
- Repetition will end on its own. A repeated process only stops when it can reach its ending condition. If nothing in the loop moves toward that condition, it runs forever as an infinite loop.
- You only need to handle the obvious cases in selection. Leaving out a range of possibilities (like temperatures between two cutoffs) means the algorithm has no plan for those inputs. Make sure your decisions cover every case you care about.
- These patterns are separate from real code. Sequencing, selection, and repetition are exactly what
ifstatements and loops carry out in Java, so the planning you do here is the same logic you will write next.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.Term | Definition |
|---|---|
algorithm | A step-by-step procedure or set of rules designed to solve a problem or accomplish a task. |
decision making | The process in an algorithm where a choice is made based on evaluating a condition to determine which path to follow. |
looping | A programming construct that repeats a block of code multiple times as part of an algorithm's repetition. |
repetition | A control structure in an algorithm where a process repeats itself until a desired outcome is reached. |
selection | A control structure in an algorithm that makes a choice about how execution will proceed based on a true or false decision. |
sequencing | The order in which steps in a process are completed, with steps executed one at a time. |
Frequently Asked Questions
What are the three building blocks of algorithms in AP CSA?
The three basic building blocks are sequencing, selection, and repetition. Sequencing means steps run in order, selection means a condition chooses whether code runs, and repetition means code repeats while a condition or stopping rule allows it.
What is selection in an algorithm?
Selection is a decision point. In Java, it usually appears as an if statement or if-else statement that checks a true/false condition before choosing which statements to run.
What is repetition in an algorithm?
Repetition means a set of statements runs more than once. In AP CSA, repetition usually appears as a for loop or while loop that continues until a condition changes or a target result is reached.
Why does order matter in an algorithm?
Order matters because each step can change the values used by later steps. When you trace code, changing the order of assignment, condition checks, or loop updates can change the final result.
How do selection and repetition become Java code?
Selection becomes if, if-else, or else-if logic. Repetition becomes loops such as for and while. AP CSA often asks you to read or write code that combines both, such as looping through values and using a condition inside the loop.
How does Topic 2.1 show up on the AP CSA exam?
Topic 2.1 supports many code tracing and code writing questions. You may need to follow the order of statements, decide when a condition is true, and predict how many times repeated steps run.