AP CSP Pseudocode Quick Reference

The exam pseudocode language, explained in plain English, with a Python translation next to every construct.

The AP CSP multiple-choice exam is written in the College Board's own pseudocode language, not Python. This page is an independent, unofficial study reference: it explains that pseudocode language construct by construct and shows the Python you'd write for the same idea, since this course is Python first.

This is not a copy of the College Board's reference sheet. Read the real one at exam time: the official AP CSP Exam Reference Sheet (PDF), via the AP CSP exam page on AP Central.

How to read the syntax boxes below:
ALL CAPS
a pseudocode keyword. Type it exactly as shown.
lowercase
a placeholder. Replace it with your own name, value, or expression.
{ }
a block containing one or more statements.
[ ]
everything inside is optional.

Assignment & input/output

variable ← expression DISPLAY (expression) variable ← INPUT ()

← stores the value on its right in the variable on its left. DISPLAY shows a value to the user. INPUT reads one value the user provides, so it's typically used on the right side of an assignment.

Pseudocode

Python

depth ← INPUT ()
DISPLAY (depth)
DISPLAY ("meters")
depth = input()
print(depth)
print("meters")

Arithmetic, MOD, RANDOM

a + b, a - b, a * b, a / b a MOD b RANDOM (a, b)

The four arithmetic symbols work as expected, and division always gives the exact numeric answer, not a truncated one. MOD gives the remainder after dividing a by b. RANDOM (a, b) gives back a random integer from a to b, and both ends are possible results.

Pseudocode

Python

buoy ← RANDOM (1, 6)
isEven ← (count MOD 2) = 0
buoy = random.randint(1, 6)
is_even = (count % 2) == 0

Relational & Boolean operators

=, ≠, >, <, ≥, ≤ NOT condition condition1 AND condition2 condition1 OR condition2

The relational operators compare two values and give back true or false. A bare = here means "is equal to", a question, not an assignment. NOT, AND, and OR combine true/false values the same way they do in everyday logic.

Pseudocode

Python

safe ← (temp ≥ 10) AND (temp ≤ 28)
IF (NOT safe)
{
  DISPLAY ("out of range")
}
safe = (temp >= 10) and (temp <= 28)
if not safe:
    print("out of range")

IF / IF-ELSE

IF (condition) { statements } [ ELSE { statements } ]

Runs the first block only when condition is true. The optional ELSE block runs instead when it's false. Another IF can go inside either block to nest conditions.

Pseudocode

Python

IF (depth > 30)
{
  DISPLAY ("deep zone")
}
ELSE
{
  IF (depth > 10)
  {
    DISPLAY ("mid zone")
  }
  ELSE
  {
    DISPLAY ("shallow zone")
  }
}
if depth > 30:
    print("deep zone")
elif depth > 10:
    print("mid zone")
else:
    print("shallow zone")

Repetition

REPEAT n TIMES { statements } REPEAT UNTIL (condition) { statements } FOR EACH item IN list { statements }

REPEAT n TIMES runs the block exactly n times. REPEAT UNTIL checks condition before every pass, including the first, and keeps looping while it's false; it stops the moment the condition becomes true. FOR EACH runs the block once per item in list, in order.

Pseudocode

Python

reading ← 100
REPEAT UNTIL (reading < 5)
{
  reading ← reading / 2
}
DISPLAY (reading)
reading = 100
while not (reading < 5):
    reading = reading / 2
print(reading)

Lists

list ← [value1, value2, value3] list[i] INSERT (list, i, value) APPEND (list, value) REMOVE (list, i) LENGTH (list)

Lists are numbered starting at 1, so list[1] is the first item and list[LENGTH(list)] is the last. APPEND adds a value to the end; INSERT puts one at position i, shifting later items over; REMOVE takes the item at position i out and shifts later items back.

Pseudocode

Python

tags ← ["A1", "A2", "A3"]
APPEND (tags, "A4")
REMOVE (tags, 1)
DISPLAY (tags[1])
tags = ["A1", "A2", "A3"]
tags.append("A4")
tags.pop(0)
print(tags[0])

Procedures

PROCEDURE name (param1, param2) { statements [ RETURN (expression) ] }

Defines a named block that can take parameters and be run later by calling name(...). Parameters are local to the procedure: changing one doesn't change whatever variable the caller passed in. RETURN hands a value back to the caller and ends the procedure immediately; a procedure with no RETURN is called for what it does, not for a value.

Pseudocode

Python

PROCEDURE average (a, b)
{
  RETURN ((a + b) / 2)
}
DISPLAY (average(18, 22))
def average(a, b):
    return (a + b) / 2

print(average(18, 22))

Robot procedures

MOVE_FORWARD () ROTATE_LEFT () ROTATE_RIGHT () CAN_MOVE (direction)

The robot sits in one square of a grid and faces one direction at a time. MOVE_FORWARD steps it one square in that direction; ROTATE_LEFT/ROTATE_RIGHT turn it 90° without moving it. CAN_MOVE(direction) checks whether the square one step away in direction is open, and returns true or false without moving the robot.

Pseudocode

Python-style

REPEAT UNTIL (NOT CAN_MOVE(DOWN))
{
  MOVE_FORWARD ()
}
IF (CAN_MOVE(RIGHT))
{
  ROTATE_RIGHT ()
  MOVE_FORWARD ()
}
while rover.can_move("down"):
    rover.move_forward()
if rover.can_move("right"):
    rover.rotate_right()
    rover.move_forward()

Gotchas for this class

Misconception: lists start at index 0, same as a Python list.

Correction: pseudocode lists start at 1. list[1] is the first item, and LENGTH(list) is the index of the last one. Add or subtract 1 when you translate to Python.

Misconception: a bare = assigns a value, because that's what it does in Python.

Correction: in this pseudocode, ← is the only assignment symbol. A bare = is always a comparison, the same job Python's == does.

Misconception: since MOD handles remainders, / must throw the remainder away too.

Correction: / always gives the exact, real-number answer. There's no separate whole-number-division operator in this pseudocode, only / and MOD.

Misconception: REPEAT UNTIL runs while its condition is true, the same feel as REPEAT n TIMES.

Correction: REPEAT UNTIL runs while the condition is false and stops once it becomes true. The condition is checked before every pass, including the first, so a condition that starts out true skips the block entirely.

Misconception: a variable created inside a PROCEDURE is visible everywhere, like a global.

Correction: variables and parameters created inside a procedure exist only for that one call. Nothing outside the procedure can see or change them, and nothing the procedure does to a parameter changes the caller's original variable.