Iteration and Robot Grid Algorithms

CSP pseudocode provides three loop constructs: (1) REPEAT n TIMES { / body / } executes the block exactly n times.

What to practice

  • Trace REPEAT n TIMES, REPEAT UNTIL(condition), and FOR EACH item IN aList loops.
  • Filter, accumulate, and search 1-indexed lists using loops.
  • Trace and design robot path-finding algorithms using MOVE_FORWARD, ROTATE_LEFT, ROTATE_RIGHT, and CAN_MOVE.

1. Core Concept Instruction

AP CSP pseudocode provides three loop constructs: (1) REPEAT n TIMES { /* body / } executes the block exactly n times. (2) REPEAT UNTIL(condition) { / body / } tests condition before each pass and repeats while condition is FALSE, terminating as soon as condition becomes TRUE! (This is the exact opposite of a Java/Python while(condition) loop, which repeats while true.) If condition is already true initially, the loop body executes 0 times; if condition never becomes true, it is an infinite loop. (3) FOR EACH item IN aList { / body */ } iterates over each element of aList from index 1 to LENGTH(aList). In Robot Grid questions, track both the robot's (row, col) square and its facing direction (North/Up, East/Right, South/Down, West/Left). MOVE_FORWARD() advances 1 square in the facing direction; ROTATE_LEFT() and ROTATE_RIGHT() turn 90 degrees in place without moving squares; and CAN_MOVE(direction) returns true if the adjacent square in direction (forward, backward, left, right) is open.

2. Worked Examples

Worked REPEAT UNTIL Trace & Robot Grid Navigation

Part 1: Trace the final value of sum and k after:
sum <- 0
k <- 1
REPEAT UNTIL (k > 4)
{
  sum <- sum + 2 * k
  k <- k + 1
}
Part 2: A robot starts at the bottom-left corner (1, 1) facing North (Up) on a 4x4 open grid and runs: REPEAT 3 TIMES { MOVE_FORWARD(); ROTATE_RIGHT(); MOVE_FORWARD(); ROTATE_LEFT() }. Find its final position and facing direction.

Result:

Robot Grid Navigation Trace with REPEAT UNTIL & CAN_MOVE(direction)

A robot starts at the bottom-left corner (row 1, col 1) facing north on a 4x4 open grid. Trace its final position and direction after:
REPEAT UNTIL (NOT CAN_MOVE(forward)) { MOVE_FORWARD() }
ROTATE_RIGHT()
MOVE_FORWARD()

Result: Robot ends at (row 4, col 2) facing east

Core Concept Review

AP CSP pseudocode provides three loop constructs: (1) REPEAT n TIMES { /* body / } executes the block exactly n times. (2) REPEAT UNTIL(condition) { / body / } tests condition before each pass and repeats while condition is FALSE, terminating as soon as condition becomes TRUE! (This is the exact opposite of a Java/Python while(condition) loop, which repeats while true.) If condition is already true initially, the loop body executes 0 times; if condition never becomes true, it is an infinite loop. (3) FOR EACH item IN aList { / body */ } iterates over each element of aList from index 1 to LENGTH(aList). In Robot Grid questions, track both the robot's (row, col) square and its facing direction (North/Up, East/Right, South/Down, West/Left). MOVE_FORWARD() advances 1 square in the facing direction; ROTATE_LEFT() and ROTATE_RIGHT() turn 90 degrees in place without moving squares; and CAN_MOVE(direction) returns true if the adjacent square in direction (forward, backward, left, right) is open.

Extended Concept Analysis

AP CSP pseudocode provides three loop constructs: (1) REPEAT n TIMES { /* body / } executes the block exactly n times. (2) REPEAT UNTIL(condition) { / body / } tests condition before each pass and repeats while condition is FALSE, terminating as soon as condition becomes TRUE! (This is the exact opposite of a Java/Python while(condition) loop, which repeats while true.) If condition is already true initially, the loop body executes 0 times; if condition never becomes true, it is an infinite loop. (3) FOR EACH item IN aList { / body */ } iterates over each element of aList from index 1 to LENGTH(aList). In Robot Grid questions, track both the robot's (row, col) square and its facing direction (North/Up, East/Right, South/Down, West/Left). MOVE_FORWARD() advances 1 square in the facing direction; ROTATE_LEFT() and ROTATE_RIGHT() turn 90 degrees in place without moving squares; and CAN_MOVE(direction) returns true if the adjacent square in direction (forward, backward, left, right) is open.

Worked Problem Walkthrough

Worked REPEAT UNTIL Trace & Robot Grid Navigation

Part 1: Trace the final value of sum and k after: sum <- 0 k <- 1 REPEAT UNTIL (k > 4) { sum <- sum + 2 * k k <- k + 1 } Part 2: A robot starts at the bottom-left corner (1, 1) facing North (Up) on a 4x4 open grid and runs: REPEAT 3 TIMES { MOVE_FORWARD(); ROTATE_RIGHT(); MOVE_FORWARD(); ROTATE_LEFT() }. Find its final position and facing direction.

Robot Grid Navigation Trace with REPEAT UNTIL & CAN_MOVE(direction)

A robot starts at the bottom-left corner (row 1, col 1) facing north on a 4x4 open grid. Trace its final position and direction after: REPEAT UNTIL (NOT CAN_MOVE(forward)) { MOVE_FORWARD() } ROTATE_RIGHT() MOVE_FORWARD()

Common Exam Traps & How to Avoid Them

  • Stopping REPEAT UNTIL (k > 4) when k = 4 instead of k = 5, or treating REPEAT UNTIL like a while loop.

  • Thinking ROTATE_LEFT() or ROTATE_RIGHT() moves the robot into an adjacent square (rotations only pivot in place!).

Key Vocabulary

  • REPEAT UNTIL(condition): A loop that repeatedly executes its body until the boolean condition evaluates to true.

  • Infinite Loop: A loop whose termination condition is never reached, causing the algorithm to run indefinitely.

Use this chapter companion

Review the lesson, attempt the original practice, then check the explanation. Your printed study guide remains complete without the website.

Independence

Skill by Skill Prep is independent. SAT® and AP® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website or book. PSAT/NMSQT® is a registered trademark of the College Board and the National Merit Scholarship Corporation, which are not affiliated with, and do not endorse, this site. Bluebook™ is a trademark of the College Board. ACT® is a trademark of ACT Education Corp., which is not affiliated with and does not endorse this website or book. Desmos is a trademark of Desmos Studio PBC. Practice results describe these questions, not official scores or admission predictions.

Last updated .