Main content
AP®︎/College Computer Science Principles
Course: AP®︎/College Computer Science Principles > Unit 9
Lesson 2: Learn AP CSP exam pseudocodeAP 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 variablea
. - 📝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
andb
. - 📝Practice: Mathematical expressions
a MOD b
- Evaluates to the remainder when a is divided by b. Assumes that
a
andb
are positive integers. - 📝Practice: Mathematical expressions
RANDOM(a, b)
- Evaluates to a random integer from
a
tob
, includinga
andb
. - 📝Practice: Random numbers
Relational and Boolean operators
a = b
a ≠ b
a > b
a < b
a ≥ b
a ≤ b
- The relational operators, =, ≠, >, <, ≥, and ≤ are used to test the relationship between two expressions, variables, or values.
- 📝Practice: Conditionals with if, else, and Booleans
NOT condition
- Evaluates to
true
ifcondition
isfalse
; otherwise evaluates tofalse
. - 📝Practice: Compound Booleans with logical operators
condition1 AND condition2
- Evaluates to
true
if bothcondition1
andcondition2
are true; otherwise evaluates tofalse
. - 📝Practice: Compound Booleans with logical operators
condition1 OR condition2
- Evaluates to
true
ifcondition1
istrue
or ifcondition2
istrue
or if bothcondition1
andcondition2
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 expressioncondition
evaluates totrue
; no action is taken ifcondition
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 expressioncondition
evaluates totrue
; otherwise the code insecond block of statements
is executed. - 📝Practice: Conditionals with if, else, and Booleans, Nested conditionals
Iteration
**
REPEAT n TIMES
{
<block of statements>
}
**
- The code in
block of statements
is executedn
times. - 📝Practice: Numbered repetition of instructions
**
REPEAT UNTIL (condition)
{
<block of statements>
}
**
- The code in
block of statements
is repeated until the Boolean expressioncondition
evaluates totrue
. - 📝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 indexi
. The first element oflist
is at index 1. - 📝Practice: Storing and updating lists
list[i] ← list[j]
- Assigns the value of
list[j]
tolist[i]
. - 📝Practice: Storing and updating lists
list ← [value1, value2, value3]
- Assigns
value1
,value2
, andvalue3
tolist[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 oflist
sequentially, in order from the first element to the last element. The code inblock 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 toi
are shifted to the right. The length oflist
is increased by 1, andvalue
is placed at indexi
inlist
. - 📝Practice: Storing and updating lists
APPEND(list, value)
- The length of
list
is increased by 1, andvalue
is placed at the end oflist
. - 📝Practice: Storing and updating lists
REMOVE(list, i)
- Removes the item at index
i
inlist
and shifts to the left any values at indices greater thani
. The length oflist
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>
}
**
- A procedure,
name
, takes zero or more parameters. The procedure contains programming instructions. - 📝Practice: Defining a procedure, Procedures with parameters
**
PROCEDURE name (parameter1, parameter2, ...)
{
<instructions>
RETURN (expression)
}
**
- A procedure,
name
, takes zero or more parameters. The procedure contains programming instructions and returns the value ofexpression
. TheRETURN
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 thedirection
relative to where the robot is facing; otherwise evaluates tofalse
. The value ofdirection
can beleft
,right
,forward
, orbackward
.
📝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?(6 votes)
- I'm fairly certain that the AP pseudocode variables are case sensitive (as it is the case with several if not most programming languages).(6 votes)