AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/2/2-1)
Unit 2 · Topic 2.1
2.1 Algorithms with Selection and Repetition
Every algorithm you'll write is built from three pieces: sequencing, selection and repetition. This topic is about spotting those pieces in everyday processes, describing them in words or a flowchart, and seeing why the order you put them in changes the result.
Key terms
- sequencing
- selection
- repetition
- flowchart
The three building blocks
Sequencing means doing steps one after another, in a set order. You met it in 1.1. Most real tasks also need the other two blocks.
Selection means making a choice. The algorithm asks a yes-or-no question, and the answer decides which steps happen next. "If it's raining, take an umbrella" is selection. In Java, the question is a Boolean expression, something that is either true or false (2.2), and the choice is made with an if statement (2.3).
Repetition means doing steps again and again until some goal is reached. "Stir until the sugar dissolves" is repetition. In Java, repetition is done with loops: while loops (2.7) and for loops (2.8). Repeating with a loop is also called iteration.
Describing an algorithm before you code it
You can write an algorithm as numbered steps in plain English, or draw it as a flowchart. In a flowchart, a rectangle is a step, a diamond is a yes-or-no question with two arrows leaving it (one for yes, one for no), and an arrow that points back up to an earlier step makes a loop.
Here's a morning routine written with all three blocks:
- 1. Turn off the alarm. (sequencing)
- 2. If it's a school day, pack your bag; otherwise, skip to step 4. (selection)
- 3. Check the bus app. While the bus is more than 5 minutes away, wait one minute and check again. (repetition)
- 4. Leave the house.
Order changes the outcome
The same steps in a different order can give a different answer. Imagine a store that takes 10 dollars off orders of 50 dollars or more and adds 8 dollars for shipping. Is the discount checked before or after shipping is added? With a 45-dollar order, checking first gives 53, because 45 doesn't qualify. Adding shipping first makes the total 53, which does qualify, so the customer pays 43.
Selection and repetition can also be combined. A loop body can contain an if, and an if body can contain a loop. Neither order is wrong on its own. The point is that the algorithm has to match what the problem says, so read the description carefully and decide where each step belongs. On the exam, many "what is printed" questions come down to tracking exactly which step happens when.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Same steps, different order
A store's rule is supposed to be: the 10-dollar discount applies only if the order itself (before shipping) is at least 50 dollars. Which of these two segments follows the rule, and what does each print? Segment A:
int total = 45; if (total >= 50) { total = total - 10; } total = total + 8; System.out.println(total);Segment B:int total = 45; total = total + 8; if (total >= 50) { total = total - 10; } System.out.println(total);Show the solutionHide the solution
- Step 1: Segment A starts with
totalat 45. The condition45 >= 50is false, so the discount is skipped. Then 8 is added: 53. It prints 53. - Step 2: Segment B adds shipping first, so
totalbecomes 53. Now53 >= 50is true, so 10 is taken off: 43. It prints 43. - Step 3: The rule says the discount depends on the order before shipping, so the check has to happen before shipping is added. That's Segment A.
Answer: Segment A prints 53 and Segment B prints 43. Segment A follows the rule.
- Step 1: Segment A starts with
- Example 2
Repetition until a goal
A culture of bacteria doubles every hour. Starting from 3 bacteria, this code counts how many hours pass before there are more than 100. What does it print?
int bacteria = 3; int hours = 0; while (bacteria <= 100) { bacteria = bacteria * 2; hours++; } System.out.println(hours + " " + bacteria);Show the solutionHide the solution
- Step 1: The loop repeats while
bacteria <= 100. Each pass doubles the count and adds 1 tohours. - Step 2: Track both values after each pass: (6, 1), (12, 2), (24, 3), (48, 4), (96, 5), (192, 6).
- Step 3: After the sixth pass,
bacteriais 192, so192 <= 100is false and the loop stops.
Answer: It prints
6 192. - Step 1: The loop repeats while
Common mistakes
- Treating selection and repetition as the same thing. An
ifchecks its condition once; a loop keeps checking and repeating. - Reading a description quickly and putting a check in the wrong place. Underline when each condition is supposed to be tested before you write the code.
- Stopping a repetition one step too early or too late. Check the last pass by hand: what are the values right when the condition becomes false?
On the exam
- Expect questions that describe a process in words and ask which code segment matches it. Check that the order of the steps matches the description, not just that the right steps are there.
Connected topics
Videos
Check yourself
3 questions on 2.1 Algorithms with Selection and Repetition. Pick an answer to see if you got it, and why.
A program follows these steps.
1. Set total to 0.
2. Read the next number.
3. If the number is positive, add it to total.
4. Go back to step 2 until 5 numbers have been read.
5. Display total.
Which step is an example of selection?
Which of the following everyday processes is the best example of repetition that continues until a goal is reached?
Two algorithms both start with x equal to 1.
Algorithm P: Add 2 to x three times. Then, if x is greater than 5, double x.
Algorithm Q: If x is greater than 5, double x. Then add 2 to x three times.
What is the final value of x in each algorithm?
0 of 3 answered