AP comp sci 2
a β expression
Evaluates
expressionand 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
aandb.πPractice: Mathematical expressions
a MOD b
Evaluates to the remainder when a is divided by b. Assumes that
aandbare positive integers.πPractice: Mathematical expressions
RANDOM(a, b)
Evaluates to a random integer from
atob, includingaandb.π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
trueifconditionisfalse; otherwise evaluates tofalse.πPractice: Compound Booleans with logical operators
condition1 AND condition2
Evaluates to
trueif bothcondition1andcondition2are true; otherwise evaluates tofalse.πPractice: Compound Booleans with logical operators
condition1 OR condition2
Evaluates to
trueifcondition1istrueor ifcondition2istrueor if bothcondition1andcondition2are true; otherwise evaluates to false.πPractice: Compound Booleans with logical operators
Selection
IF (<condition>) { <block of statements> }
The code in
block of statementsis executed if the Boolean expressionconditionevaluates totrue; no action is taken ifconditionevaluates 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 statementsis executed if the Boolean expressionconditionevaluates totrue; otherwise the code insecond block of statementsis executed.πPractice: Conditionals with if, else, and Booleans, Nested conditionals
Iteration
** REPEAT n TIMES { <block of statements> }
**
The code in
block of statementsis executedntimes.πPractice: Numbered repetition of instructions
** REPEAT UNTIL (condition) { <block of statements> }
**
The code in
block of statementsis repeated until the Boolean expressionconditionevaluates 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
listat indexi. The first element oflistis 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, andvalue3tolist[1],list[2],list[3], respectively.πPractice: Storing and updating lists
FOR EACH item IN list { <block of statements> }
The variable
itemis assigned the value of each element oflistsequentially, in order from the first element to the last element. The code inblock of statementsis executed once for each assignment of item.πPractice: Iterating over lists with loops
INSERT (list, i, value)
Any values in
listat indices greater than or equal toiare shifted to the right. The length oflistis increased by 1, andvalueis placed at indexiinlist.πPractice: Storing and updating lists
APPEND(list, value)
The length of
listis increased by 1, andvalueis placed at the end oflist.πPractice: Storing and updating lists
REMOVE(list, i)
Removes the item at index
iinlistand shifts to the left any values at indices greater thani. The length oflistis 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. TheRETURNstatement 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.
CAN_MOVE (direction)
Evaluates to
trueif there is an open square one square in thedirectionrelative to where the robot is facing; otherwise evaluates tofalse. The value ofdirectioncan beleft,right,forward, orbackward.