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.
Courte vidéo de cours
Examen blanc
Quiz d'entraînement
Teste-toi : résultats et explications immédiats.
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:
Besoin d'aide sur ce sujet ?
Lors d'un appel de découverte gratuit de 30 minutes, nous évaluons ton niveau et construisons un plan d'étude sur mesure.
Appel de découverte gratuit