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.

### Course: AP®︎/College Computer Science Principles>Unit 9

Lesson 2: Learn AP CSP exam pseudocode

# 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`
• Evaluates `expression` and assigns the result to the variable `a`.
• 📝Practice: Storing data in variables
`DISPLAY (expression)`
• Displays the value of `expression`, followed by a space.
• 📝Practice: Programming basics
`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 ```
• Evaluates to `true` if both `condition1` and `condition2` are true; otherwise evaluates to `false`.
• 📝Practice: Compound Booleans with logical operators
``` condition1 OR condition2 ```
• Evaluates to `true` if `condition1` is `true` or if `condition2` is `true` or if both `condition1` and `condition2` are true; otherwise evaluates to false.
• 📝Practice: Compound Booleans with logical operators

#### 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> } ```
• The code in `first block of statements` is executed if the Boolean expression `condition` evaluates to `true`; otherwise the code in `second block of statements` is executed.
• 📝Practice: Conditionals with if, else, and Booleans, Nested conditionals

#### Iteration

** ``` REPEAT n TIMES { <block of statements> } ```
**
** ``` REPEAT UNTIL (condition) { <block of statements> } ```
**
• The code in `block of statements` is repeated until the Boolean expression `condition` evaluates to `true`.
• 📝Practice: Conditional repetition of instructions

#### 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]`
• Refers to the element of `list` at index `i`. The first element of `list` is at index 1.
• 📝Practice: Storing and updating lists
`list[i] ← list[j]`
`list ← [value1, value2, value3]`
• Assigns `value1`, `value2`, and `value3` to `list[1]`, `list[2]`, `list[3]`, respectively.
• 📝Practice: Storing and updating lists
``` 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) ```
• The length of `list` is increased by 1, and `value` is placed at the end of `list`.
• 📝Practice: Storing and updating lists
``` 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?

• is the ap style pseudocode case sensitive between variable names? is List the same as list?