AP Computer Science A · Study guide

Selection and Repetition

Unit 2 · Selection and Iteration · Topic 2.1

Selection and Repetition

The three building blocks every algorithm is made of — and why putting them in a different order gives you a different program.

Exam-day recap
  • The building blocks of algorithms are sequencing, selection, and repetition.
  • Sequencing defines an order for when steps are completed — "first this, then that."
  • Selection chooses how execution proceeds based on a true-or-false decision — "if this is true, do that."
  • Repetition repeats a process until a desired outcome is reached — "keep doing this until."
  • Selection is always binary: the decision is true or not — no third branch, no partial credit.
  • A vague condition like "if the game felt sloppy" is not selection until it can be evaluated as true or false.
  • A repetition needs a checkable finish line; drop it and the process never ends — an infinite loop.
  • The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
  • The classic trap: selection inside a repetition runs every pass, but after it runs only once.
  • Sequencing alone changes the outcome: adding then counting includes the new item; counting then adding does not.

1Three Building Blocks

This topic comes before any Java syntax, and deliberately so. Every algorithm you will ever write — and most of the ones you follow without noticing — is assembled from exactly three things.

Essential knowledge 2.1.A.1

The building blocks of algorithms include sequencing, selection, and repetition.

BlockWhat it doesIn plain language
SequencingDefines an order for when steps are completed"first this, then that"
SelectionChooses how execution proceeds, based on a true-or-false decision"if this is true, do that"
RepetitionRepeats a process until a desired outcome is reached"keep doing this until"
The learning objective

2.1.A — represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams. Note what it does not say: nothing about Java. You are describing algorithms in words, and the exam skill paired with it is determining the result based on the order statements execute in.

2An Everyday Algorithm

T1's practice block, written the way the course wants it — plain language, no code:

Scrim routine
  1. Doran, Oner, Faker, Peyz and Keria load into the lobby.
  2. Play a scrim game.
  3. If the team lost, review the replay together.
  4. Repeat from step 2 until five games have been played.
  5. End the block and post the day's notes.

All three blocks are present, and each is identifiable:

StepBlockWhy
1 → 2 → 5SequencingAn order is defined; steps happen one at a time
3Selection"If the team lost" is a true-or-false decision that changes what happens next
4RepetitionA process repeats until a desired outcome — five games — is reached

2.1 Selection Is Always Binary

Essential knowledge 2.1.A.3

Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.

"If the team lost" is either true or it is not. There is no third branch and no partial credit. That matters because it is what makes selection mechanical — a step anyone, or any machine, can carry out identically.

Something like "review the replay if the game felt sloppy" is not selection in this sense. "Felt sloppy" cannot be evaluated as true or false without a judgment call, so the algorithm is not fully specified. Turning a vague condition into a testable one is most of the work of writing an algorithm.

Not yet a decisionA true-or-false decision
"if the game felt sloppy""if the team lost"
"if Faker played well""if Faker's deaths were fewer than 3"
"if there is enough time""if fewer than five games have been played"

2.2 Repetition Needs a Finish Line

Essential knowledge 2.1.A.4

Repetition is when a process repeats itself until a desired outcome is reached.

"Until five games have been played" is a finish line: it can be checked, and it will eventually arrive. Drop it and the routine never ends — the team scrims forever. In an algorithm on paper that reads as sloppiness; in a program it is an infinite loop, and it is the same mistake either way.

The question to ask of any repetition

What makes this stop, and does that thing actually change? "Until five games have been played" works because each pass plays a game, so the count moves toward five. A repetition whose stopping condition never changes will never stop, no matter how reasonable it reads.

3Order Changes the Outcome

Essential knowledge 2.1.A.5

The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.

This is the most examinable sentence in the topic. The same three blocks, rearranged, describe a genuinely different routine.

3.1 Selection Inside the Repetition

Version A
  1. Repeat five times:
    1. Play a scrim game.
    2. If the team lost, review the replay.

The decision is made every pass. Five games, and a review after each loss — up to five reviews.

3.2 Selection After the Repetition

Version B
  1. Repeat five times:
    1. Play a scrim game.
  2. If the team lost more than they won, review the replays.

The decision is made once, after all five games. Same blocks, same words almost, and a different routine: a team that loses three straight then wins two reviews once in Version B (it lost more than it won), and reviews three times in Version A.

Version AVersion B
Decision evaluatedFive timesOnce
Reviews possible0 to 50 or 1
Feedback arrivesImmediately after each lossAt the end of the block
What the exam does with this

You will be given two algorithms built from the same steps and asked whether they produce the same result. The answer turns on where the selection sits relative to the repetition — inside means once per pass, outside means once in total. Trace both against the same data before answering; they diverge on inputs that look ordinary.

3.3 Sequencing Alone Can Change Everything

Even with no selection or repetition involved, order decides the outcome:

Order 1Order 2
Add Peyz to the roster, then count the rosterCount the roster, then add Peyz
Count includes PeyzCount does not

The course states that steps in a process are completed one at a time, and that is exactly why this matters. Each step sees the state left by the step before it.

4Representing an Algorithm

The learning objective allows two forms: written language or diagrams. Both are acceptable; the requirement is that the representation be unambiguous.

The same routine as a diagram description

A box for each action, a diamond for each decision with two labeled exits, and an arrow looping back for repetition. Version A loops the arrow around both the game and the decision; Version B loops it around the game only, with the diamond placed after the loop closes. The visual difference is exactly the behavioral one.

Writing one that holds up
  1. One action per step. "Play a game and review it" hides two steps and a possible decision.
  2. Every condition true-or-false. If you cannot answer it with yes or no, rewrite it.
  3. Every repetition ends. Name the finish line and check that something moves toward it.
  4. Say where a decision lives. Inside the repetition or after it — indentation or a diagram makes this explicit; prose often hides it.

5Check Yourself

Which block is "if Keria has vision score above 80, log the game as a benchmark"?

Response

Selection. The choice of how execution proceeds rests on a decision that is either true or false — the vision score is above 80, or it is not.

An algorithm says "keep reviewing replays until the team improves." What is wrong with it?

Response

The repetition has no usable finish line. "The team improves" cannot be evaluated as true or false, so there is no way to know whether to stop. Repetition must run until a desired outcome that can actually be checked — "until the team wins three consecutive scrims" would work.

Two algorithms contain the same play step, the same review step, and the same five-game repetition, but one puts the review decision inside the loop and one puts it after. Same outcome?

Response

No. Inside the loop the decision is evaluated once per game, so a review can happen up to five times. After the loop it is evaluated once, so a review happens at most once. The order in which selection and repetition are combined contributes to the outcome — which is the point of essential knowledge 2.1.A.5.

Is sequencing still a building block if an algorithm has no decisions and no loops?

Response

Yes, and it is the only one present. Sequencing defines the order steps are completed in, and steps are completed one at a time. An algorithm of five plain steps is entirely sequencing — and reordering those five steps can still change the result.

6Summary

IdeaWhat to remember
The three blocksSequencing, selection, repetition
SequencingDefines the order; steps complete one at a time
SelectionExecution proceeds based on a true or false decision
RepetitionA process repeats until a desired outcome is reached
Order mattersThe arrangement of the blocks contributes to the outcome
The classic trapSelection inside a repetition runs every pass; after it, once
RepresentationWritten language or diagrams — both fine, both must be unambiguous
Vague conditionsNot selection until they can be answered yes or no
The habit

Given any everyday routine, name the three blocks in it before writing a line of anything. Where does the order matter, which decisions are genuinely true-or-false, and what stops each repetition? An algorithm that survives those three questions in plain English will survive being turned into code.