# AP® Computer Science A 2.1 Algorithms with Selection and Repetition

Source: https://lockincs.com/ap-csa/unit-2/2-1-algorithms-with-selection-and-repetition/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 2.1.A, 2.1.A.1, 2.1.A.2, 2.1.A.3, 2.1.A.4, 2.1.A.5

The building blocks of algorithms are sequencing, selection and repetition. Selection chooses how the algorithm proceeds based on a true or false decision, and repetition repeats a process until a desired outcome is reached. The order in which you combine the three changes the outcome.

## What you will learn

- 2.1.A Represent patterns and algorithms with selection and repetition from everyday life, using written language or diagrams.

## 1\. The three building blocks

An algorithm is a step-by-step process that solves a problem or completes a task. Every algorithm, from a recipe to a video game, is built from three building blocks: sequencing, selection and repetition 2.1.A.1.

| Building block | What it does | Everyday example | Java tool (later in Unit 2) |
| --- | --- | --- | --- |
| Sequencing | Runs steps one after another, in order | Put on socks, then shoes | Statements in order |
| Selection | Makes a decision, then picks which steps run | If it is raining, take an umbrella | `if`, `if-else` |
| Repetition | Repeats steps until a goal is reached | Keep stirring until the sugar dissolves | `while`, `for` |

Algorithms get selection through decision making and repetition through looping 2.1.A.2. Here is an everyday algorithm, written in plain language, that uses all three:

1.  Take the cap off the bottle. (sequencing)
2.  If the bottle is dirty, rinse it. (selection)
3.  Hold the bottle under the fountain until it is full. (repetition)
4.  Put the cap back on. (sequencing)

## 2\. Selection: a true or false decision

Selection happens when the way an algorithm continues depends on a true or false decision 2.1.A.3. The question must have exactly two possible answers. “Is the temperature above 25 degrees?” works. “What is the temperature?” does not, because its answer is a number, not true or false.

| Decision | If true | If false |
| --- | --- | --- |
| Is the temperature above 25? | Wear shorts | Wear jeans |

The same decision in Java (you will learn the `if` statement in Topic 2.3):

```
int temperature = 31;
if (temperature > 25) {
    System.out.println("Wear shorts");
} else {
    System.out.println("Wear jeans");
}
```

Output:

```
Wear shorts
```

**Watch out:** only one of the two paths runs. The program never prints both lines.

## 3\. Repetition: repeat until the desired outcome

In repetition, the same process runs again and again, and it stops once the result you want has been reached 2.1.A.4. In plain language: “Water the plant one cup at a time until the soil moisture reaches 50.”

```
int moisture = 20;
int cups = 0;
while (moisture < 50) {
    moisture += 15;
    cups++;
}
System.out.println("Cups of water: " + cups);
```

Output:

```
Cups of water: 2
```

Trace it. Each row is one check of the decision “is moisture still below 50?”

| Check | moisture < 50? | moisture after the step | cups |
| --- | --- | --- | --- |
| 1 | 20 < 50 is true | 35 | 1 |
| 2 | 35 < 50 is true | 50 | 2 |
| 3 | 50 < 50 is false | (stop) | 2 |

**Watch out:** the repeated step must move toward the desired outcome. If the plant never got any water, the moisture would stay at 20 and the process would never end.

## 4\. Order changes the outcome

Where you place each step of sequencing, selection and repetition helps decide what the algorithm produces, so changing the order can change the result 2.1.A.5. Look at two versions of a game rule. A player has 90 points, earns 15 bonus points, and levels up at 100 points.

Version A checks first, then adds the bonus:

```
int points = 90;
if (points >= 100) {
    System.out.println("Level up!");
}
points += 15;
System.out.println("Points: " + points);
```

Output:

```
Points: 105
```

Version B adds the bonus first, then checks:

```
int points = 90;
points += 15;
if (points >= 100) {
    System.out.println("Level up!");
}
System.out.println("Points: " + points);
```

Output:

```
Level up!
Points: 105
```

Same steps, same final points, different behavior. In Version A the decision saw the old value (90), so the player never leveled up.

## 5\. From written steps to code

Writing the algorithm in plain language first helps you choose the right building blocks. Problem: a student saves 12 dollars each week for headphones that cost 50 dollars. How many weeks does it take, and how much extra money is left?

1.  Set saved to 0 and weeks to 0.
2.  Repeat until saved is at least 50: add 12 to saved and add 1 to weeks.
3.  Print the number of weeks.
4.  If saved is more than 50, print the extra money.

Each numbered step becomes code:

```
int saved = 0;
int weeks = 0;
while (saved < 50) {
    saved += 12;
    weeks++;
}
System.out.println("Weeks: " + weeks);
if (saved > 50) {
    System.out.println("Extra: " + (saved - 50));
}
```

Output:

```
Weeks: 5
Extra: 10
```

Notice the translation: “repeat until saved is at least 50” becomes “keep looping while saved is less than 50”. The loop continues while the opposite of the goal is true.

## Common mix-ups

- Not true: Selection means choosing between many options at once. Each selection is a single true or false decision. A choice among many options is built from several true or false decisions checked one after another.
- Not true: Repetition always runs a fixed number of times. Repetition runs until a desired outcome is reached. Sometimes you know the count in advance (10 laps), and sometimes you do not (stir until the soup is warm).
- Not true: If an algorithm has the right steps, their order does not matter. The same steps in a different order can give a different result. Checking a score before or after adding bonus points can change what the program prints.

## In 30 seconds

- Algorithms are built from sequencing, selection and repetition.
- Selection is decision making: a true or false decision picks which steps run next.
- Repetition is looping: a process repeats until a desired outcome is reached.
- Changing the order of the building blocks can change the result.
- You can write an algorithm as numbered steps in plain language before you write any Java.

## Checkpoint: 5 practice questions

### Question 1

A student is writing an algorithm for a game. Which of the following questions can be used directly as a selection decision in the algorithm?

- A. What is the player's score?
- B. Is the player's score greater than 100?
- C. How many lives does the player have left?
- D. Which level should the player play next?

**Answer: B.** Selection is based on a true or false decision, and only "Is the player's score greater than 100?" has a true or false answer. The other three questions ask for a number or a choice of level, so they must first be turned into a true or false test (for example, "Are there 0 lives left?") before they can control a selection.

### Question 2

Consider the following algorithm, written in plain language.

1. Fill the kettle.
2. Switch the kettle on.
3. Wait 10 seconds, then check the water; keep doing this until the water boils.
4. If you want tea, add a tea bag to the cup.
5. Pour the water into the cup.

Which step uses repetition?

- A. Step 4
- B. Step 1
- C. Step 5
- D. Step 3

**Answer: D.** Step 3 repeats a process (wait, then check) until a desired outcome is reached (the water boils), which is repetition. Step 4 is selection, a true or false decision about tea. Steps 1 and 5 run once, in order, which is sequencing.

### Question 3

A game uses two steps. Step P adds 10 points to the player's score. Step Q prints "Winner" if the score is at least 50. A player starts with 45 points. Which of the following best describes what happens?

- A. Running P then Q prints "Winner", but running Q then P does not.
- B. Running Q then P prints "Winner", but running P then Q does not.
- C. Both orders print "Winner", because the final score is 55 either way.
- D. Neither order prints "Winner", because 45 is less than 50.

**Answer: A.** The order of sequencing and selection changes the outcome. If P runs first, Q checks 55, which is at least 50, so it prints "Winner". If Q runs first, it checks 45 and prints nothing, even though the final score is still 55. A decision only sees the value at the moment it runs, which is why "both orders" is wrong.

### Question 4

Consider the following code segment. What is printed as a result of executing the code segment?

```java
int stamina = 4;
int moves = 0;
while (stamina > 0) {
    moves++;
    stamina -= 2;
    if (stamina == 0) {
        stamina += 1;
    }
}
System.out.println(moves);
```

- A. 2
- B. 4
- C. 3
- D. Nothing is printed because the loop never ends.

**Answer: C.** Trace: stamina 4 gives moves 1 and stamina 2; stamina 2 gives moves 2 and stamina 0, which the selection changes to 1; stamina 1 gives moves 3 and stamina -1, and the loop ends. Answering 2 ignores the selection inside the repetition. The loop does end, because stamina keeps going down overall.

### Question 5

A recipe says: "If the dough is sticky, add a spoon of flour." Which statement best describes this step?

- A. It is repetition: flour is added until the dough is not sticky.
- B. It is sequencing: the step always runs after the previous step.
- C. It is selection: the step runs only when a true or false condition is true.
- D. It is repetition and selection, because the condition is checked again and again.

**Answer: C.** The step makes one true or false decision (is the dough sticky?) and runs at most once, which is selection. It would be repetition only if it said to keep adding flour until the dough is no longer sticky. It is not plain sequencing, because the step does not always run.

## Questions students ask

**What are the three building blocks of an algorithm in AP® Computer Science A?**

Sequencing (steps run in order), selection (a true or false decision chooses what runs next) and repetition (a process repeats until a desired outcome is reached).

**What is the difference between selection and repetition?**

Selection decides once which path to take. Repetition goes back and runs a process again and again until the desired outcome is reached.

**Why does the order of steps matter in an algorithm?**

Each step works with the values left by the steps before it. Moving a decision before or after an update means the decision sees a different value, so the outcome can change.

**Do I need to know Java to answer 2.1 questions?**

Topic 2.1 is about representing algorithms in written language or diagrams. Later topics in Unit 2 turn the same ideas into Java if statements and loops.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
