AP Computer Science Principles Exam Reference Sheet Study Guide

AP Computer Science Principles Exam Reference Sheet Overview The AP Computer Science Principles Exam Reference Sheet, dated July 2015, is a guiding document designed to assist students in understanding the format and logic of the questions they will encounter on the exam. Since the AP Computer Science Principles course does not mandate the use of a specific programming language, this reference sheet serves as a universal standard, providing instructions and explanations for two primary programming formats: text-based and block-based. These instructions utilize four fundamental data types: numbers, Booleans, strings, and lists. The categories of instructions covered by the sheet include Assignment, Display, and Input; Arithmetic Operators and Numeric Procedures; Relational and Boolean Operators; Selection; Iteration; List Operations; Procedures; and Robot instructions. # Assignment, Display, and Input The reference sheet details three primary operations for handling data flow within a program. The first is assignment, denoted in text as aexpressiona \leftarrow \text{expression}. This operation evaluates the given expression and assigns the resulting value to the variable aa. The second operation is display, written as DISPLAY (expression)\text{DISPLAY (expression)}. This command outputs the value of the expression, and notably, it is followed by a space. The third operation is input, written as INPUT ()\text{INPUT ()}, which accepts a value provided by the user and returns that value for use within the program. # Arithmetic Operators and Numeric Procedures Mathematical operations on the exam include standard arithmetic and specific numeric procedures. The basic operators for addition, subtraction, multiplication, and division are represented in both text and block formats as a+ba + b, aba - b, a×ba \times b, and a/ba / b. For example, the division operation 3/23 / 2 evaluates to the numeric value 1.51.5. Another vital operator is the modulo operator, represented as a MOD ba \text{ MOD } b. This operation evaluates to the remainder when the integer aa is divided by the integer bb. The reference sheet assumes that both aa and bb are positive integers; for instance, the expression 17 MOD 517 \text{ MOD } 5 evaluates to 22. Additionally, the sheet defines the procedure RANDOM (a, b)\text{RANDOM (a, b)}, which evaluates to a random integer starting from aa and going up to and including bb. For example, the execution of RANDOM (1, 3)\text{RANDOM (1, 3)} results in either 11, 22, or 33. # Relational and Boolean Operators Relational operators are utilized to test relationships between variables, expressions, or values. These include equality represented as a=ba = b (which evaluates to true if the values are equal and false otherwise), inequality represented as aba \neq b, greater than represented as a>ba > b, less than represented as a<ba < b, greater than or equal to represented as aba \geq b, and less than or equal to represented as aba \leq b. Boolean logic is handled through three operators: NOT condition\text{NOT condition} evaluates to true if the condition is false and false if the condition is true. The condition1 AND condition2\text{condition1 AND condition2} operator evaluates to true only if both conditions are true; otherwise, it is false. Finally, condition1 OR condition2\text{condition1 OR condition2} evaluates to true if either condition1\text{condition1} is true, condition2\text{condition2} is true, or if both are true; it only evaluates to false if both individual conditions are false. # Selection and Iteration Selection instructions allow a program to branch based on Boolean conditions. The basic IF (condition)\text{IF (condition)} structure executes a block of statements only if the condition evaluates to true; if false, no action is taken. The IF (condition) ELSE\text{IF (condition) ELSE} structure provides two paths: the first block of statements is executed if the condition is true, while the second block of statements is executed if the condition is false. Iteration instructions control the repetitive execution of code. The REPEAT n TIMES\text{REPEAT n TIMES} structure evaluates the block of statements exactly nn times. The REPEAT UNTIL (condition)\text{REPEAT UNTIL (condition)} structure repeats a block of statements continuously until the specified Boolean condition evaluates to true. # List Operations Lists are structured data sets where indices represent positioning. A critical rule for all list operations is that if a list index is less than 11 or greater than the actual length of the list, an error message is produced and the program terminates immediately. The reference sheet specifies that the first element of a list is located at index 11. Specific operations include list[i]\text{list[i]}, which refers to the element at index ii, and list[i]list[j]\text{list[i]} \leftarrow \text{list[j]}, which assigns the value of the element at index jj to the position at index ii. A list can be initialized as list[value1, value2, value3]\text{list} \leftarrow [\text{value1, value2, value3}], assigning the values to symbols at indices 11, 22, and 33 respectively. The FOR EACH item IN list\text{FOR EACH item IN list} loop assigns the variable item\text{item} the value of each element in the list sequentially from the first to the last, executing the block of statements once for each assignment. Modification commands include INSERT (list, i, value)\text{INSERT (list, i, value)}, which shifts all values at indices greater than or equal to ii to the right, increases the list length by 11, and places the new value at index ii. The APPEND (list, value)\text{APPEND (list, value)} command increases the list length by 11 and adds the value to the very end of the list. REMOVE (list, i)\text{REMOVE (list, i)} deletes the item at index ii, shifts all subsequent values to the left, and decreases the list length by 11. The LENGTH (list)\text{LENGTH (list)} function returns the total number of elements currently in the list. # Procedures Procedures are defined blocks of code that can take zero or more parameters. The text syntax is PROCEDURE name (parameter1, parameter2, ...)\text{PROCEDURE name (parameter1, parameter2, ...)} followed by a block of instructions. Procedures may also return a value using the RETURN (expression)\text{RETURN (expression)} statement. This statement can appear at any point within the procedure; when executed, it causes an immediate return from the procedure back to the calling program, passing back the evaluated value of the expression. # Robot Instructions The Robot category defines how a simulated robot interacts with a grid. If the robot attempts to move to a square that is not open or is beyond the grid’s edge, the robot remains in its current location and the program terminates. The command \text{MOVE_FORWARD ()} moves the robot one square in its current direction. \text{ROTATE_LEFT ()} turns the robot 9090^{\circ} counterclockwise, while \text{ROTATE_RIGHT ()} turns it 9090^{\circ} clockwise. The \text{CAN_MOVE (direction)} function evaluates to true if there is an open square in the specified direction relative to the robot's current facing; otherwise, it returns false. Valid directions for this function are left\text{left}, right\text{right}, forward\text{forward}, or backward\text{backward}.