Algorithms aren't just code - they're the logical patterns we use to solve problems every day. When you decide what to wear based on the weather (selection) or keep hitting snooze until you absolutely have to get up (repetition), you're using algorithmic thinking. Understanding these patterns before diving into Java syntax helps you see that programming is really about translating human logic into a language computers understand.
The three building blocks - sequencing, selection, and repetition - combine to create every algorithm you'll ever write. Sequencing is just doing steps in order, which we've been doing all along. Selection adds decision-making power ("if this, then that"), while repetition handles tasks that need to happen multiple times. Master these three concepts and you can solve virtually any computational problem.
What makes algorithms powerful is how these simple pieces combine. A morning routine algorithm might repeat getting dressed until you find a clean shirt (repetition), then select breakfast based on time available (selection), all in a specific sequence. Once you recognize these patterns in everyday life, translating them to code becomes much more intuitive.
- Major concepts: Three building blocks (sequencing, selection, repetition), decision-making in algorithms, loops for repeated processes, combining patterns
- Why this matters for AP: Foundation for all control structures, essential for FRQ1, helps plan solutions before coding
- Common pitfalls: Forgetting that order matters, infinite loops in repetition, incomplete selection cases
- Key vocabulary: Algorithm, sequencing, selection, repetition, decision-making, iteration
- Prereqs: Understanding of basic algorithms from Topic 1.1, logical thinking
Key Concepts

The Three Building Blocks of Algorithms
Every algorithm, no matter how complex, is built from just three fundamental patterns. Sequencing means executing steps in a specific order - first this, then that. It's the default mode of algorithms where one action follows another in a predetermined sequence.
Selection introduces decision-making into algorithms. Based on some condition being true or false, the algorithm chooses which path to follow. This is how algorithms adapt to different situations instead of blindly following the same steps every time.
Repetition allows algorithms to perform tasks multiple times without writing the same code over and over. The algorithm repeats a process until some desired outcome is reached, whether that's processing every item in a list or continuing until the user says stop.
Selection - Making Decisions in Algorithms
Selection is all about branching paths based on conditions. When an algorithm encounters a selection point, it evaluates a condition and proceeds down one path if true, another if false. This is like a fork in the road where you must choose a direction.
Real-world examples of selection are everywhere. A vending machine selects which product to dispense based on your button press. Your phone selects silent or ring mode based on a switch. These decisions follow an if-then pattern that's fundamental to algorithmic thinking.
The key to selection is that it's based on a true/false decision. There's no maybe or probably - the condition either is or isn't met. This binary nature makes selection reliable and predictable in algorithms.
Repetition - Doing Things Multiple Times
Repetition in algorithms means performing a process repeatedly until you achieve the desired outcome. This could be a fixed number of times (repeat 10 times) or until some condition is met (repeat until done). Without repetition, algorithms would be extremely limited and repetitive to write.
Think about washing dishes - you repeat the wash-rinse-dry process for each dish until all dishes are clean. The "until all dishes are clean" part is crucial. Repetition needs a clear ending condition, or it continues forever (infinite loop).
Repetition makes algorithms efficient and flexible. Instead of writing separate instructions for each dish, you write the process once and repeat it. This same principle applies whether you're processing a list of numbers or checking every pixel in an image.
Combining Patterns for Complex Algorithms
Real algorithms rarely use just one pattern. They combine sequencing, selection, and repetition in various ways. You might have repetition inside selection (if it's raining, keep checking every 5 minutes), or selection inside repetition (for each student, if grade > 90, mark as honor roll).
The order of combination matters tremendously. Checking if you're done before or after the main process can completely change the outcome. This is why algorithm design requires careful thought about not just what to do, but in what order.
Consider a password verification algorithm: it uses selection (is password correct?), repetition (allow multiple attempts), and sequencing (check attempt count before allowing another try). The interplay of these patterns creates the complete solution.
Code Examples
Let's see these patterns in everyday algorithms before we code them:
// 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! } }
Real-world algorithm patterns:
// Example: ATM withdrawal algorithm pattern public class ATMAlgorithm { // This shows the pattern, not actual code public static void describeATMProcess() { // SEQUENCING starts the process System.out.println("1. Insert card"); System.out.println("2. Enter PIN"); // SELECTION for PIN verification System.out.println("3. IF (PIN correct) THEN proceed"); System.out.println(" ELSE show error and return card"); // REPETITION for menu options System.out.println("4. REPEAT"); System.out.println(" Show menu options"); System.out.println(" Get user choice"); System.out.println(" Process choice"); System.out.println(" UNTIL (user selects 'Exit')"); // SELECTION within repetition System.out.println("5. For each transaction:"); System.out.println(" IF (sufficient funds) THEN complete"); System.out.println(" ELSE show insufficient funds message"); } }
Algorithm for finding a book in library:
// Example: Library book search algorithm public class LibrarySearch { public static void explainBookSearch() { System.out.println("Algorithm: Find a specific book"); System.out.println(); // SEQUENCING - Initial steps System.out.println("1. Go to correct section (by subject)"); System.out.println("2. Find correct shelf (by call number range)"); // REPETITION with SELECTION inside System.out.println("3. REPEAT for each book on shelf:"); System.out.println(" Look at book spine"); System.out.println(" IF (this is the book) THEN"); System.out.println(" Take book and stop searching"); System.out.println(" ELSE"); System.out.println(" Move to next book"); System.out.println(" UNTIL (found book OR reached end of shelf)"); // SELECTION after repetition System.out.println("4. IF (book not found) THEN"); System.out.println(" Check if book is checked out"); System.out.println(" OR ask librarian for help"); } }
Common Errors and Debugging
Infinite Loops in Repetition
The most dangerous error with repetition is creating a loop that never ends:
// PROBLEM: What's wrong with this algorithm? // REPEAT // Check if homework is done // If not done, procrastinate // UNTIL homework is done // The issue: Nothing in the loop actually does homework! // This will repeat forever because the condition never becomes true
Always ensure your repetition includes steps that progress toward the ending condition.
Missing Cases in Selection
Selection errors occur when you don't handle all possible cases:
// PROBLEM: Incomplete selection // IF (temperature > 70) THEN // Wear shorts // ELSE IF (temperature < 40) THEN // Wear coat // What happens when temperature is between 40 and 70? // BETTER: Cover all cases // IF (temperature > 70) THEN // Wear shorts // ELSE IF (temperature >= 40) THEN // Wear long pants // ELSE // Wear coat
Wrong Order of Operations
The sequence of steps matters enormously:
// PROBLEM: Wrong order // 1. Put on shoes // 2. Put on socks // Obviously this won't work! // PROBLEM: Checking condition at wrong time // REPEAT // Eat cookie // Check if full // UNTIL full // This eats one cookie even if already full! // BETTER: Check first // REPEAT // IF (not full) THEN // Eat cookie // UNTIL full
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 (decisions based on preferences)
- Repetition: Step 4 (loops for multiple toppings)
- Combination: Selection happens after sequencing, repetition contains implied selection
Problem 2: Design an algorithm for a simple guessing game where the computer picks a number and the player guesses until correct.
Solution:
1. Computer picks random 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's wrong with this algorithm for filling a swimming pool?
REPEAT Turn on water Wait 1 hour UNTIL pool looks full Turn off water
Solution: Major problem - the water gets turned on repeatedly! The "turn on water" should be before the loop:
Turn on water // Do this once REPEAT Wait 1 hour Check pool level UNTIL pool is full Turn off water
AP Exam Connections
This topic is foundational for understanding all the control structures you'll implement in Java. While you won't write pseudocode on the exam, you need to think algorithmically to solve FRQ problems. Understanding these patterns helps you plan your solution before writing code.
For multiple choice, you'll trace through algorithms that combine these patterns. Questions might ask what happens when selection and repetition interact, or how changing the order affects the outcome.
In FRQs:
- FRQ 1 (Methods/Control): Always uses selection (if statements) and often repetition (loops)
- FRQ 2 (Class Design): Methods might use selection to validate data
- FRQ 3 (Array/ArrayList): Heavy use of repetition to process elements
- FRQ 4 (2D Array): Nested repetition for rows and columns
Key tip: Before writing any code on the FRQ, think about the algorithm pattern. Do you need to make decisions (selection)? Process multiple items (repetition)? This planning phase helps you organize your solution and avoid logic errors. The graders can see clear algorithmic thinking even if your syntax isn't perfect.
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. |