AP comp sci 2

a ← expression

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

  • 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

condition1 AND condition2

condition1 OR condition2

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> }

Iteration

** REPEAT n TIMES { <block of statements> }

**

** REPEAT UNTIL (condition) { <block of statements> }

**

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]

list[i] ← list[j]

list ← [value1, value2, value3]

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)

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.

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.