If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

AP CSP exam pseudocode reference

Since AP CS Principles is taught with a variety of programming languages, the AP CSP exam questions use a pseudocode that represents fundamental programming concepts.
Each AP CSP exam comes with a pseudocode reference that students can consult during the exam. That reference is available on page 205 of the College Board AP CSP exam description.
To practice AP CSP pseudocode, you can work through the exercises in our Programming and Algorithms units. You can see a reference to the pseudocode used in each question by clicking "What language is this code in?" under the answers.
The following is an adaptation of the official pseudocode reference, with links to practice questions for each concept.

AP CSP pseudocode

Assignment, display, and input

a ← expression
DISPLAY (expression)
INPUT ()
  • Accepts a value from the user and returns it.

Arithmetic operators and numeric procedures

a + b a - b a * b a / b
  • The arithmetic operators, +, -, *, and /, are used to perform arithmetic on a and b.
  • 📝Practice: Mathematical expressions
a MOD b
  • Evaluates to the remainder when a is divided by b. Assumes that a and b are positive integers.
  • 📝Practice: Mathematical expressions
RANDOM(a, b)
  • Evaluates to a random integer from a to b, including a and b.
  • 📝Practice: Random numbers

Relational and Boolean operators

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

Selection

IF (<condition>) { <block of statements> }
  • The code in block of statements is executed if the Boolean expression condition evaluates to true; no action is taken if condition evaluates to false.
  • 📝Practice: Conditionals with if, else, and Booleans
IF (<condition>) { <first block of statements> } ELSE { <second block of statements> }

Iteration

** REPEAT n TIMES { <block of statements> }
**
** REPEAT UNTIL (condition) { <block of statements> }
**

List operations

For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced and the program terminates.
list[i]
list[i] ← list[j]
list ← [value1, value2, value3]
FOR EACH item IN list { <block of statements> }
  • The variable item is assigned the value of each element of list sequentially, in order from the first element to the last element. The code in block of statements is executed once for each assignment of item.
  • 📝Practice: Iterating over lists with loops
INSERT (list, i, value)
  • Any values in list at indices greater than or equal to i are shifted to the right. The length of list is increased by 1, and value is placed at index i in list.
  • 📝Practice: Storing and updating lists
APPEND(list, value)
REMOVE(list, i)
  • Removes the item at index i in list and shifts to the left any values at indices greater than i. The length of list is decreased by 1.
  • 📝Practice: Storing and updating lists
LENGTH(list)
  • Evaluates to the number of elements in list.

Procedures

** PROCEDURE name (parameter1, parameter2, ...) { <instructions> }
**
** PROCEDURE name (parameter1, parameter2, ...) { <instructions> RETURN (expression) }
**
  • A procedure, name, takes zero or more parameters. The procedure contains programming instructions and returns the value of expression. The RETURN statement may appear at any point inside the procedure and causes an immediate return from the procedure back to the calling program.
  • 📝Practice: Procedures with return values

Robot

If the robot attempts to move to a square that is not open or is beyond the edge of the grid, the robot will stay in its current location and the program will terminate.
MOVE_FORWARD ()
  • The robot moves one square forward in the direction it is facing.
ROTATE_LEFT ()
  • The robot rotates in place 90 degrees counterclockwise (i.e. makes an in-place left turn).
ROTATE_RIGHT ()
  • The robot rotates in place 90 degrees clockwise (i.e. makes an in-place right turn).
CAN_MOVE (direction)
  • Evaluates to true if there is an open square one square in the direction relative to where the robot is facing; otherwise evaluates to false. The value of direction can be left, right, forward, or backward.
📝Practice of robot-like questions are throughout the Repetition lesson.

Want to join the conversation?