Algorithms & Pseudocode
AP Computer Science Principles — algorithms and the three building blocks (sequencing, selection, iteration) using College Board exam-reference pseudocode. Worked example and exam tip.
An algorithm is a finite set of instructions that accomplishes a task. Every algorithm — no matter how complex — is built from just three control structures. AP CSP uses College Board exam-reference pseudocode, so learn its notation.
The three building blocks
- Sequencing — steps run in order, top to bottom.
- Selection — a decision with
IF/ELSE. - Iteration — repetition with
REPEAT n TIMESorREPEAT UNTIL (condition).
Exam-reference pseudocode
- Assignment uses a left arrow:
a ← 5 - Output:
DISPLAY(expression)Input:INPUT() MODgives the remainder:7 MOD 3is1
```
total ← 0
REPEAT 5 TIMES
{
total ← total + 2
}
DISPLAY(total)
```
This displays 10.
Selection example
```
IF (score ≥ 60)
{ DISPLAY("Pass") }
ELSE
{ DISPLAY("Fail") }
```
Worked example
What does n MOD 2 = 0 test? It is true when n is even (remainder 0 when divided by 2) — a very common CSP pattern.
Exam tip
You must be able to trace pseudocode by hand and predict the output, and recognise when two algorithms are equivalent. Watch loop counts carefully (REPEAT 5 TIMES runs exactly 5 times). Also know linear vs binary search and that some problems are "undecidable" or only solvable in reasonable (polynomial) vs unreasonable (exponential) time.
Short Lesson Video
Mock Exam
Practice Quiz
Test yourself: instant results and explanations.
1. Starting with total ← 0, what does REPEAT 5 TIMES { total ← total + 2 } display?
2. The condition n MOD 2 = 0 is true when n is:
3. The three building blocks of every algorithm are:
Need support with this topic?
In a free 30-minute intro call we assess your level and build a study plan tailored to you.
Free intro call