AP · Computer Science Principles · Procedures and Abstraction

Robot on a Grid

12 min readUpdated December 1, 2026FreeAP Computer Science Principles, Algorithms and Programming

Robot questions give you a grid, a robot facing one way, and code built from four procedures on the exam reference sheet. MOVE_FORWARD() moves the robot one square the way it faces, the two rotations turn it 90 degrees in place, and CAN_MOVE(direction) tells you whether the square that way, measured from the robot, is open. In AP® Computer Science Principles, you answer them by tracing the robot's square and facing after every command.

Quick check

  1. x starts at 1, and REPEAT UNTIL(x > 3) { x ← x + 1 } runs. How many times does the body run?Show answerHide answer

    3 times. x goes 1 → 2 → 3 → 4, and the loop stops once x > 3 is true.

  2. A robot faces north and turns right twice. Which way does it face now?Show answerHide answer

    South. One right turn makes it face east, and a second makes it face south.

Know these already? Jump to the practice questions

What you'll be able to do

  • State what each of the four robot procedures does.
  • Determine a robot's square and facing after a code segment runs.
  • Explain how CAN_MOVE measures direction from the robot.
  • Decide which code segment moves the robot to a goal square.

Key terms

Relative direction

Left, right, forward or backward measured from the way the robot faces, not from the page.

In plain English
The robot's own left, not yours.
Don't write
"CAN_MOVE(left) checks the square to the west."

What do the four robot procedures do?

All four are on the exam reference sheet.

ProcedureWhat it doesSquare changes?Facing changes?
MOVE_FORWARD()moves one square the way the robot facesyesno
ROTATE_LEFT()turns 90 degrees to the robot's leftnoyes
ROTATE_RIGHT()turns 90 degrees to the robot's rightnoyes
CAN_MOVE(direction)true if the next square in direction (left, right, forward or backward) is opennono

This note draws each grid as a table. Rows count from 1 at the top and columns from 1 at the left, so (5, 1) is row 5, column 1. North is toward row 1. ■ is a blocked square, R the robot's start and G the goal.

Check yourself

Which robot procedure is the only one that changes the robot's square?
Show answerHide answer

MOVE_FORWARD(). The two rotations change only the facing, and CAN_MOVE only reports a value.

How do turns change the way the robot faces?

Follow the compass. Each ROTATE_RIGHT() moves one step clockwise: north → east → south → west → north. Each ROTATE_LEFT() moves one step counterclockwise: north → west → south → east → north.

Two shortcuts save time:

  • Two turns either way make the robot face the opposite direction.
  • Three left turns equal one right turn, and three right turns equal one left turn.

Key idea

Rotations change only the facing; only MOVE_FORWARD() changes the square.

Check yourself

A robot faces west and runs ROTATE_RIGHT() once. Which way does it face?
Show answerHide answer

North. Clockwise from west is north.

How does CAN_MOVE decide left and right?

The direction in CAN_MOVE is relative: it is measured from the way the robot faces. Turn it into a compass point first.

Robot facesforwardrightbackwardleft
northnortheastsouthwest
easteastsouthwestnorth
southsouthwestnortheast
westwestnortheastsouth

A robot facing east that tests CAN_MOVE(left) is checking the square to its north. The result is false if that square is blocked or off the grid.

REPEAT UNTIL(NOT CAN_MOVE(forward)) { MOVE_FORWARD() } moves the robot ahead until the next square is blocked or past the edge.

Check yourself

A robot faces south. Which compass direction does CAN_MOVE(left) check?
Show answerHide answer

East. Facing south, the robot's left side points east.

How do you choose the segment that reaches the goal?

Trace every option; never judge by how a segment looks.

  1. Note the start square and facing.
  2. Trace one command at a time: the command, the square, the facing.
  3. Reject an option at its first move into a blocked square or off the grid: a correct segment never tries one.
  4. Check the final square, and the facing if the question asks.

A REPEAT n TIMES loop with the wrong n leaves the robot a square short or a square too far, and a REPEAT UNTIL loop can carry it past the goal.

Check yourself

An option ends on the goal square but moves through a blocked square on the way. Is it correct?
Show answerHide answer

No. A correct segment never tries to move into a blocked square, wherever it ends.

Worked example 1: Trace a sequence of moves

Question: On a 5 × 5 grid with no blocked squares, the robot starts in (5, 1) facing north. Squares are (row, column), with row 1 at the north edge. After this code segment runs, give the robot's (a) row, (b) column and (c) facing (north, east, south or west).

MOVE_FORWARD()
MOVE_FORWARD()
ROTATE_RIGHT()
REPEAT 3 TIMES
{
  MOVE_FORWARD()
}
ROTATE_LEFT()
MOVE_FORWARD()

What tells you the method

A fixed list of moves and turns: trace the square and facing after each command.

  1. Step 1: Trace every command.

    CommandSquareFacing
    start(5, 1)north
    MOVE_FORWARD() × 2(3, 1)north
    ROTATE_RIGHT()(3, 1)east
    MOVE_FORWARD() × 3(3, 4)east
    ROTATE_LEFT()(3, 4)north
    MOVE_FORWARD()(2, 4)north
    Why

    Moving north lowers the row number; moving east raises the column number.

Check it

The robot moved 3 squares north and 3 east in total, and (5, 1) moved 3 rows up and 3 columns right is (2, 4).

Answer

(a) 2 (b) 4 (c) north

Worked example 2: A loop with CAN_MOVE

Question: The robot starts in (4, 1) facing north. After this code segment runs, give the robot's (a) row, (b) column and (c) facing (north, east, south or west).

12345
1
2■
3■
4R
5
REPEAT 4 TIMES
{
  IF(CAN_MOVE(forward))
  {
    MOVE_FORWARD()
  }
  ELSE
  {
    ROTATE_RIGHT()
  }
}

What tells you the method

A loop whose body depends on CAN_MOVE: trace each pass, checking the square ahead first.

  1. Step 1: Trace the four passes.

    PassSquare aheadCAN_MOVE(forward)Square, facing after
    1(3, 1)true(3, 1), north
    2(2, 1)false(3, 1), east
    3(3, 2)true(3, 2), east
    4(3, 3)true(3, 3), east
    Why

    (2, 1) is blocked, so pass 2 turns right instead of moving.

Check it

A fifth pass would find (3, 4) blocked and turn the robot south, so after exactly 4 passes it still faces east.

Answer

(a) 3 (b) 3 (c) east

Worked example 3: Which segment reaches the goal?

Question: The robot starts in (5, 1) facing north. Which code segment moves it to the goal G at (1, 4) without trying to enter a blocked square or leave the grid?

12345
1G
2
3■■
4■
5R
  • A) REPEAT 3 TIMES { MOVE_FORWARD() }, ROTATE_RIGHT(), REPEAT 3 TIMES { MOVE_FORWARD() }
  • B) REPEAT 4 TIMES { MOVE_FORWARD() }, ROTATE_RIGHT(), REPEAT 3 TIMES { MOVE_FORWARD() }
  • C) REPEAT 4 TIMES { MOVE_FORWARD() }, ROTATE_LEFT(), REPEAT 3 TIMES { MOVE_FORWARD() }
  • D) ROTATE_RIGHT(), REPEAT 3 TIMES { MOVE_FORWARD() }, ROTATE_LEFT(), REPEAT 4 TIMES { MOVE_FORWARD() }

What tells you the method

A "which segment" question: trace each option and reject it at its first bad move.

  1. Step 1: Trace each option.

    OptionWhat happens
    Anorth to (2, 1), east to (2, 4): one row short
    Bnorth to (1, 1), east to (1, 4): reaches G
    Cnorth to (1, 1), faces west: next move leaves the grid
    Deast to (5, 4), north to (4, 4): next move enters blocked (3, 4)
    Why

    Only B keeps every move on an open square and ends on G.

Check it

B passes through column 1 and row 1 only, and no square there is blocked.

Answer

B

Your turn

Question: The robot starts in (5, 2) facing north. After this code segment runs, give the robot's (a) row, (b) column and (c) facing (north, east, south or west).

12345
1
2■
3
4
5R
REPEAT UNTIL(NOT CAN_MOVE(forward))
{
  MOVE_FORWARD()
}
ROTATE_LEFT()
MOVE_FORWARD()
  1. Step 1: Trace the loop.

    The robot moves to (4, 2), then (3, 2). There, the square ahead, (2, 2), is blocked, so the loop stops.

  2. Step 2: Your go: trace ROTATE_LEFT() and the last MOVE_FORWARD().

Show the full solutionHide the solution

Step 2. ROTATE_LEFT() turns the robot from north to west without moving it. MOVE_FORWARD() then moves it west from (3, 2) to (3, 1), which is open.

Answer: (a) 3 (b) 1 (c) west

Traps

Trap

ROTATE_LEFT() makes the robot face the left side of the page.

It seems right because left on the page is west. But the turn is to the robot's own left: a robot facing south that turns left then faces east.

A robot faces south and runs ROTATE_LEFT(). Which way does it face?

East.

Trap

CAN_MOVE(right) checks the square to the east.

It seems right because east is right on the page. But the direction is measured from the robot: facing west, its right side points north.

A robot faces west. Which compass direction does CAN_MOVE(right) check?

North.

What's examined

On the AP® Computer Science Principles exam, robot questions belong to Big Idea 3: Algorithms and Programming. They ask where the robot ends or which code segment reaches a goal. The four robot procedures are on the exam reference sheet; the tracing method is Learn this.

Practice questions

  1. Recall Which procedure call can change the square the robot is in?
  • A) MOVE_FORWARD()
  • B) ROTATE_LEFT()
  • C) ROTATE_RIGHT()
  • D) CAN_MOVE(forward)
Show answer

Answer: A. It moves the robot one square in the direction it faces.

Why the others are wrong:

  • B turns the robot in place.
  • C turns the robot in place.
  • D only reports whether a move is possible.
  1. Apply A robot faces east and runs ROTATE_LEFT() three times. Which way does it face? Answer north, east, south or west.
Show answer

Answer: south

Marking points:

  • East → north → west → south, one counterclockwise step per turn.
  • Three left turns equal one right turn, and right of east is south.
  1. Apply On a 3 × 3 grid, the robot is in (2, 2) facing south. Squares are (row, column), with row 1 at the north edge. Squares (2, 3) and (1, 2) are blocked. What do (a) CAN_MOVE(left) and (b) CAN_MOVE(right) evaluate to? Answer true or false for each.
Show answer

Answer: (a) false (b) true

Marking points:

  • Facing south, left is east: (2, 3) is blocked, so false.
  • Facing south, right is west: (2, 1) is open, so true.
  1. Exam-style The robot starts in (5, 1) facing east. Which code segment moves it to the goal G at (2, 3) without trying to enter a blocked square or leave the grid?
12345
1
2G
3■
4■
5R■
  • A) REPEAT UNTIL(NOT CAN_MOVE(forward)) { MOVE_FORWARD() }, ROTATE_RIGHT(), REPEAT 3 TIMES { MOVE_FORWARD() }
  • B) REPEAT UNTIL(NOT CAN_MOVE(forward)) { MOVE_FORWARD() }, ROTATE_LEFT(), REPEAT 2 TIMES { MOVE_FORWARD() }
  • C) REPEAT UNTIL(NOT CAN_MOVE(forward)) { MOVE_FORWARD() }, ROTATE_LEFT(), REPEAT 3 TIMES { MOVE_FORWARD() }
  • D) ROTATE_LEFT(), REPEAT 3 TIMES { MOVE_FORWARD() }, ROTATE_RIGHT(), REPEAT UNTIL(NOT CAN_MOVE(forward)) { MOVE_FORWARD() }
Show answer

Answer: C. The loop stops at (5, 3) because (5, 4) is blocked; the left turn faces north, and three moves reach (2, 3).

Why the others are wrong:

  • A turns right, to face south, so its next move leaves the grid.
  • B repeats one move too few and ends at (3, 3).
  • D reaches (2, 1), then its loop runs east past G to (2, 5).
  1. Exam-style The code segment below should move the robot from (5, 1), facing north, to (3, 3) on a 5 × 5 grid with no blocked squares. Squares are (row, column), with row 1 at the north edge. Give the number of the line that stops it working.
1: REPEAT 2 TIMES
2: {
3:   MOVE_FORWARD()
4: }
5: ROTATE_LEFT()
6: REPEAT 2 TIMES
7: {
8:   MOVE_FORWARD()
9: }
Show answer

Answer: Line 5.

Marking points:

  • Lines 1 to 4 bring the robot to (3, 1) facing north.
  • ROTATE_LEFT() faces it west, so the next move would leave the grid; ROTATE_RIGHT() faces it east, and two moves reach (3, 3).
  1. Apply The robot starts in (5, 3) facing north on a 5 × 5 grid where only (1, 3) is blocked. Squares are (row, column), with row 1 at the north edge. How many times does the body of REPEAT UNTIL(NOT CAN_MOVE(forward)) { MOVE_FORWARD() } run?
Show answer

Answer: 3

Marking points:

  • The robot moves to (4, 3), (3, 3) and (2, 3).
  • At (2, 3) the square ahead, (1, 3), is blocked, so the loop stops.

Key points

Recall, then reveal

Say the main points from memory first, then check them.

Show key pointsHide key points
  • Only MOVE_FORWARD() changes the robot's square; the rotations turn it 90 degrees in place.
  • Right turns go clockwise around the compass, left turns counterclockwise.
  • CAN_MOVE directions are measured from the robot, so convert them to compass points first.
  • To pick the segment that reaches the goal, trace every option and reject any that tries a blocked or off-grid move.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Algorithms and Programming

All Procedures and Abstraction notes

Part of Novark's AP Computer Science Principles notes