Iteration

Programming Fundamentals
structure

Also known as: loop, repetition

Grade 6-8

View on concept map

Repeating a block of instructions multiple times until a stopping condition is satisfied. Iteration powers processing of lists, searching, sorting, and any repeated action.

Definition

Repeating a block of instructions multiple times until a stopping condition is satisfied. Iteration is one of the three fundamental control structures (along with sequence and selection) and is implemented through while loops, for loops, and other looping constructs.

💡 Intuition

Do this again and again until some condition is met, then move on to the next step.

🎯 Core Idea

Loops let you do repetitive tasks without writing repetitive code.

Example

Stir soup until it boils. Print numbers 1 to 100 using a loop instead of 100 print lines.

🌟 Why It Matters

Iteration powers processing of lists, searching, sorting, and any repeated action. Without loops, processing a list of 1000 items would require 1000 separate lines of code. Loops make programs scalable to any data size.

💭 Hint When Stuck

When writing a loop, first decide what action needs repeating and what condition should stop the repetition. Initialize any loop variables before the loop starts, and make sure the loop body changes something so the stopping condition is eventually reached.

Formal View

Iteration executes a statement block S repeatedly while a boolean condition B holds: WHILE B DO S. The loop invariant is a property that holds true before and after each iteration.

🚧 Common Stuck Point

The loop must have a way to stop—every iteration should progress toward the stopping condition.

⚠️ Common Mistakes

  • Forgetting to update the loop variable, causing an infinite loop that never terminates
  • Off-by-one errors where the loop runs one too many or one too few times
  • Modifying the collection being iterated over during the loop, causing skipped or duplicate elements

Common Mistakes Guides

Frequently Asked Questions

What is Iteration in CS Thinking?

Repeating a block of instructions multiple times until a stopping condition is satisfied. Iteration is one of the three fundamental control structures (along with sequence and selection) and is implemented through while loops, for loops, and other looping constructs.

When do you use Iteration?

When writing a loop, first decide what action needs repeating and what condition should stop the repetition. Initialize any loop variables before the loop starts, and make sure the loop body changes something so the stopping condition is eventually reached.

What do students usually get wrong about Iteration?

The loop must have a way to stop—every iteration should progress toward the stopping condition.

Prerequisites

How Iteration Connects to Other Ideas

To understand iteration, you should first be comfortable with sequence. Once you have a solid grasp of iteration, you can move on to while loop, for loop and recursion.

💻 Animated Visualization Animated

Watch the loop counter increment