Assignment & input/output
variable ← expression DISPLAY (expression) variable ← INPUT ()
← stores the value on its right in the variable on its left.
DISPLAY shows a value to the user. INPUT reads one
value the user provides, so it's typically used on the right side of an
assignment.
Pseudocode
Python
depth ← INPUT ()
DISPLAY (depth)
DISPLAY ("meters")
depth = input()
print(depth)
print("meters")
Arithmetic, MOD, RANDOM
a + b, a - b, a * b, a / b a MOD b RANDOM (a, b)
The four arithmetic symbols work as expected, and division always gives the
exact numeric answer, not a truncated one. MOD gives the
remainder after dividing a by b.
RANDOM (a, b) gives back a random integer from a to
b, and both ends are possible results.
Pseudocode
Python
buoy ← RANDOM (1, 6) isEven ← (count MOD 2) = 0
buoy = random.randint(1, 6) is_even = (count % 2) == 0
Relational & Boolean operators
=, ≠, >, <, ≥, ≤ NOT condition condition1 AND condition2 condition1 OR condition2
The relational operators compare two values and give back true or false.
A bare = here means "is equal to", a question, not an
assignment. NOT, AND, and OR combine
true/false values the same way they do in everyday logic.
Pseudocode
Python
safe ← (temp ≥ 10) AND (temp ≤ 28)
IF (NOT safe)
{
DISPLAY ("out of range")
}
safe = (temp >= 10) and (temp <= 28)
if not safe:
print("out of range")
IF / IF-ELSE
IF (condition) { statements } [ ELSE { statements } ]
Runs the first block only when condition is true. The
optional ELSE block runs instead when it's false. Another
IF can go inside either block to nest conditions.
Pseudocode
Python
IF (depth > 30)
{
DISPLAY ("deep zone")
}
ELSE
{
IF (depth > 10)
{
DISPLAY ("mid zone")
}
ELSE
{
DISPLAY ("shallow zone")
}
}
if depth > 30:
print("deep zone")
elif depth > 10:
print("mid zone")
else:
print("shallow zone")
Repetition
REPEAT n TIMES { statements } REPEAT UNTIL (condition) { statements } FOR EACH item IN list { statements }
REPEAT n TIMES runs the block exactly n times.
REPEAT UNTIL checks condition before every pass,
including the first, and keeps looping while it's false; it stops the
moment the condition becomes true. FOR EACH runs the block
once per item in list, in order.
Pseudocode
Python
reading ← 100
REPEAT UNTIL (reading < 5)
{
reading ← reading / 2
}
DISPLAY (reading)
reading = 100
while not (reading < 5):
reading = reading / 2
print(reading)
Lists
list ← [value1, value2, value3] list[i] INSERT (list, i, value) APPEND (list, value) REMOVE (list, i) LENGTH (list)
Lists are numbered starting at 1, so list[1] is the first
item and list[LENGTH(list)] is the last. APPEND
adds a value to the end; INSERT puts one at position
i, shifting later items over; REMOVE takes the
item at position i out and shifts later items back.
Pseudocode
Python
tags ← ["A1", "A2", "A3"] APPEND (tags, "A4") REMOVE (tags, 1) DISPLAY (tags[1])
tags = ["A1", "A2", "A3"]
tags.append("A4")
tags.pop(0)
print(tags[0])
Procedures
PROCEDURE name (param1, param2) { statements [ RETURN (expression) ] }
Defines a named block that can take parameters and be run later by
calling name(...). Parameters are local to the procedure:
changing one doesn't change whatever variable the caller passed in.
RETURN hands a value back to the caller and ends the
procedure immediately; a procedure with no RETURN is called
for what it does, not for a value.
Pseudocode
Python
PROCEDURE average (a, b)
{
RETURN ((a + b) / 2)
}
DISPLAY (average(18, 22))
def average(a, b):
return (a + b) / 2
print(average(18, 22))
Robot procedures
MOVE_FORWARD () ROTATE_LEFT () ROTATE_RIGHT () CAN_MOVE (direction)
The robot sits in one square of a grid and faces one direction at a
time. MOVE_FORWARD steps it one square in that direction;
ROTATE_LEFT/ROTATE_RIGHT turn it 90°
without moving it. CAN_MOVE(direction) checks whether the
square one step away in direction is open, and returns true
or false without moving the robot.
Pseudocode
Python-style
REPEAT UNTIL (NOT CAN_MOVE(DOWN))
{
MOVE_FORWARD ()
}
IF (CAN_MOVE(RIGHT))
{
ROTATE_RIGHT ()
MOVE_FORWARD ()
}
while rover.can_move("down"):
rover.move_forward()
if rover.can_move("right"):
rover.rotate_right()
rover.move_forward()