AP CSP Exam
Reference Sheet
AP COMPUTER SCIENCE PRINCIPLES
Effective
Fall 2020
THIS PAGE IS INTENTIONALLY LEFT BLANK.
Exam Reference Sheet
Instruction Explanation
Assignment, Display, and Input
Tex t :
a expression
Block:
a
expression
Evaluates expression and then assigns a copy of the result to
the variable
a.
Tex t :
DISPLAY(expression)
Block:
DISPLAY expression
Displays the value of expression, followed by a space.
Tex t :
INPUT()
Block:
INPUT
Accepts a value from the user and returns the input value.
Arithmetic Operators and Numeric Procedures
Text and Block:
a + b
a - b
a * b
a / b
The arithmetic operators +, -, *, and / are used to perform
arithmetic on
a and b.
For example,
17 / 5 evaluates to 3.4.
The order of operations used in mathematics applies when evaluating
expressions.
Text and Block:
a MOD b
Evaluates to the remainder when a is divided by b. Assume that
a is an integer greater than or equal to 0 and b is an integer
greater than
0.
For example,
17 MOD 5 evaluates to 2.
The
MOD operator has the same precedence as the * and /
operators.
Tex t :
RANDOM(a, b)
Block:
RANDOM a,
b
Generates and returns a random integer from a to b, including
a and b. Each result is equally likely to occur.
For example,
RANDOM(1, 3) could return 1, 2, or 3.
Relational and Boolean Operators
Text and Block:
a = b
a ≠ b
a > b
a < b
a ≥ b
a ≤ b
The relational operators =,, >, <,, andare used to test
the relationship between two variables, expressions, or values. A
comparison using relational operators evaluates to a Boolean value.
For example,
a = b evaluates to true if a and b are
equal; otherwise it evaluates to
false.
AP Computer Science Principles
Exam Reference V.1
|
1
Return to Table of Contents
© 2023 College Board
Instruction Explanation
Relational and Boolean Operators (continued)
Tex t :
NOT condition
Block:
NOT condition
Evaluates to true if condition is false; otherwise
evaluates to
false.
Tex t :
condition1 AND condition2
Block:
condition1 condition2AND
Evaluates to true if both condition1 and condition2
are true; otherwise evaluates to false.
Tex t :
condition1 OR condition2
Block:
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.
Selection
Tex t :
IF(condition)
{
<block of statements>
}
Block:
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.
Tex t :
IF(condition)
{
<rst block of statements>
}
ELSE
{
<second block of statements>
}
Block:
IF condition
first block of statements
ELSE
second block of statements
The code in rst block of statements is executed
if the Boolean expression
condition evaluates to true;
otherwise the code in
second block of statements is
executed.
AP Computer Science Principles
© 2023 College Board
Exam Reference V.1
|
2
Return to Table of Contents
Instruction Explanation
Iteration
Tex t :
REPEAT n TIMES
{
<block of statements>
}
Block:
REPEAT n TIMES
block of statements
The code in block of statements is executed n times.
Tex t :
REPEAT UNTIL(condition)
{
<block of statements>
}
Block:
REPEAT UNTIL
block of statements
condition
The code in block of statements is repeated until the
Boolean expression
condition evaluates to true.
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.
Tex t :
aList [value1, value2, value3, ...]
Block:
aList valuel, value2, value3
Creates a new list that contains the values value1, value2,
value3, and ... at indices 1, 2, 3, and ...
respectively and assigns it to aList.
Tex t :
aList []
Block:
aList
Creates an empty list and assigns it to aList.
Tex t :
aList bList
Block:
aList bList
Assigns a copy of the list bList to the list aList.
For example, if
bList contains [20, 40, 60],
then
aList will also contain [20, 40, 60] after the
assignment.
Tex t :
aList[i]
Block:
aList
i
Accesses the element of aList at index i. The rst element
of
aList is at index 1 and is accessed using the notation
aList[1].
AP Computer Science Principles
© 2023 College Board
Exam Reference V.1
|
3
Return to Table of Contents
Instruction Explanation
List Operations (continued)
Tex t :
x aList[i]
Block:
x aList i
Assigns the value of aList[i] to the variable x.
Tex t :
aList[i] x
Block:
aList
ix
Assigns the value of x to aList[i].
Tex t :
aList[i] aList[j]
Block:
aListaList
ij
Assigns the value of aList[j] to aList[i].
Tex t :
INSERT(aList, i, value)
Block:
aList, i, valueINSERT
Any values in aList at indices greater than or equal to i are
shifted one position to the right. The length of the list is increased by
1, and
value is placed at index i in aList.
Tex t :
APPEND(aList, value)
Block:
APPEND
aList, value
The length of aList is increased by 1, and value is placed at
the end of
aList.
Tex t :
REMOVE(aList, i)
Block:
REMOVE aList, i
Removes the item at index i in aList and shifts to the left
any values at indices greater than
i. The length of aList is
decreased by 1.
Tex t :
LENGTH(aList)
Block:
LENGTH
aList
Evaluates to the number of elements in aList.
Tex t :
FOR EACH item IN aList
{
<block of statements>
}
Block:
FOR EACH item IN aList
block of statements
The variable item is assigned the value of each element of
aList sequentially, in order, from the rst element to the last
element. The code in
block of statements is executed
once for each assignment of
item.
AP Computer Science Principles
© 2023 College Board
Exam Reference V.1
|
4
Return to Table of Contents
Instruction Explanation
Procedures and Procedure Calls
Tex t :
PROCEDURE procName(parameter1,
parameter2, ...)
{
<block of statements>
}
Block:
PROCEDURE procName
block of statements
parameter1,
parameter2,...
Denes procName as a procedure that takes zero or more
arguments. The procedure contains
block of statements.
The procedure
procName can be called using the following
notation, where
arg1 is assigned to parameter1, arg2 is
assigned to
parameter2, etc.:
procName(arg1, arg2, ...)
Tex t :
PROCEDURE procName(parameter1,
parameter2, ...)
{
<block of statements>
RETURN(expression)
}
Block:
PROCEDURE procName
block of statements
RETURN expression
parameter1,
parameter2,...
Denes procName as a procedure that takes zero or more
arguments. The procedure contains
block of statements
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
statement.
The value returned by the procedure
procName can be assigned
to the variable
result using the following notation:
result procName(arg1, arg2, ...)
Tex t :
RETURN(expression)
Block:
RETURN
expression
Returns the ow of control to the point where the procedure was
called and returns the value of
expression.
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.
Tex t :
MOVE_FORWARD()
Block:
MOVE_FORWARD
The robot moves one square forward in the direction it is facing.
Tex t :
ROTATE_LEFT()
Block:
ROTATE_LEFT
The robot rotates in place 90 degrees counterclockwise (i.e., makes
an in-place left turn).
AP Computer Science Principles
© 2023 College Board
Exam Reference V.1
|
5
Return to Table of Contents
Instruction Explanation
Robot
Tex t :
ROTATE_RIGHT()
Block:
ROTATE_RIGHT
The robot rotates in place 90 degrees clockwise (i.e., makes an in-
place right turn).
Tex t :
CAN_MOVE(direction)
Block:
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.
AP Computer Science Principles
© 2023 College Board
Exam Reference V.1
|
6
Return to Table of Contents