Iteration & Loops (College Board AP® Computer Science Principles): Study Guide

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Iteration & loop types

What is iteration?

  • Iteration is a repeating portion of an algorithm

  • It repeats a specified number of times, or until a given condition is met

  • Iteration statements change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met

  • AP CSP uses two loop structures: REPEAT n TIMES and REPEAT UNTIL

REPEAT n TIMES

  • Executes the loop body a fixed number of times, determined before the loop runs

  • Used when the number of repetitions is known in advance

REPEAT 3 TIMES
{
   DISPLAY("Hello")
}
 
  • This displays "Hello" exactly three times

REPEAT UNTIL

  • Executes the loop body repeatedly until a condition becomes true

  • The condition is evaluated before each iteration

  • If the condition of a REPEAT UNTIL loop is already true when the loop starts, the body does not execute at all, the condition is checked before each iteration, including the first one

  • Used when the number of repetitions depends on a changing value or user input

count ← 1
REPEAT UNTIL(count > 5)
{
   DISPLAY(count)
   count ← count + 1
}
 
  • This displays 1 through 5, then stops when count exceeds 5

Loop type

When to use

Number of iterations

REPEAT n TIMES

Number of repetitions is known in advance

Fixed

REPEAT UNTIL

Repetitions depend on a condition

Variable

Loop behavior

How does loop termination work?

  • A loop terminates (stops) when its stopping condition is met

  • REPEAT n TIMES terminates automatically after exactly n iterations

  • REPEAT UNTIL terminates when its condition evaluates to true

Infinite loops

  • An infinite loop occurs when the condition of a REPEAT UNTIL loop never becomes true

  • The program appears to freeze and continues looping indefinitely

  • Common causes:

    • The condition variable is never updated inside the loop body

    • The condition is logically impossible to satisfy

count ← 1
REPEAT UNTIL(count = 0)
{
   count ← count + 1
}
 
  • This loop is infinite because count increases each iteration and will never equal 0

  • Every loop must have a clear, reachable stopping condition; if the condition variable never changes inside the loop body, the loop will run indefinitely

Examiner Tips and Tricks

  • REPEAT UNTIL stops when the condition is true, not false — read the condition carefully in code trace questions. For REPEAT n TIMES, the loop body always runs exactly n times; nothing inside the body can cut it short. In infinite loop questions, check whether the condition variable actually changes inside the loop body — if it does not, the loop will never terminate

  • For the AP Create Performance Task, iteration is commonly used to process lists — if your program uses a list, a loop makes your solution more efficient and generalizable

Worked Example

What will the following code segment display?

x ← 1
REPEAT UNTIL(x > 3)
{
   DISPLAY(x)
   x ← x + 1
}
 

(A) 1 2 3 4
(B) 1 2 3
(C) 2 3 4
(D) The loop runs infinitely

[1]

Answer:

(B) 1 2 3 [1 mark]

  • The condition is checked before each iteration. Starting from x = 1: display 1, increment to 2. Display 2, increment to 3. Display 3, increment to 4. Now the condition x > 3 is true, so the loop terminates before displaying 4.

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.