StayTalentReady

Iteration and Loops

Outcome 4: Iteration and Loops · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx

Objectives

Key terms

for loop
A loop that iterates over every item in a sequence (range, list, string) for a known number of iterations.
while loop
A loop that repeats its body as long as a condition remains True; the number of iterations is not known in advance.
range()
A built-in function generating an integer sequence: range(n) → 0..n-1; range(a,b) → a..b-1; range(a,b,step).
iteration
One pass through the loop body; for i in range(5) has 5 iterations.
break
Immediately exits the enclosing loop; execution resumes after the loop.
continue
Skips the rest of the current iteration and jumps to the next; the loop condition is checked again.
accumulator
A variable initialized before a loop (total = 0) and updated each iteration (total += score).
infinite loop
A while loop whose condition is never False; the program never exits the loop on its own.
loop variable
The variable (i, n, item) that the for statement assigns each sequence value on each iteration.

The concept

Iteration is how programs do repetitive work efficiently. Without loops, printing numbers 1 through 100 would require 100 print statements. With a for loop, it takes two lines.

The for loop iterates over a sequence. range(5) generates 0, 1, 2, 3, 4 — five values. for i in range(5): print(i) prints each. range(start, stop) generates integers from start up to but not including stop. range(1, 11) gives 1 through 10. range(start, stop, step) skips by step: range(0, 20, 5) gives 0, 5, 10, 15. The loop variable (i) takes each value in turn. You can also iterate a list directly: for score in scores: print(score) prints every element.

The while loop repeats while a condition is True. It is correct when you do not know how many iterations are needed in advance — waiting for user input, searching until found, running a menu until the user exits. Every while loop must have a statement inside the body that eventually makes the condition False. Forgetting this creates an infinite loop — the program runs forever (press Ctrl+C to stop).

break and continue control loop flow. break exits the loop entirely — useful for searching (stop when found). continue skips to the next iteration — useful for filtering (skip invalid values). Neither keyword is required in every loop; use them when they make the code clearer, not as a substitute for writing a correct condition.

The accumulator pattern is one of the most common programming patterns. Before the loop: total = 0. Inside the loop: total += each_item. After the loop: use total. This computes a running sum. To count: count = 0 before, count += 1 inside when a condition is met.

Blueprint Pillar 3 — Technology and Society: Loops are at the heart of every digital operation. Search engines loop through billions of pages, streaming services loop through recommendation lists, and fraud detection systems loop through millions of transactions every second. The ability to write correct, efficient loops — ones that terminate and produce accurate results — is fundamental to every computing career.

Worked examples

Example 1: Sum of list using accumulator: scores = [85, 92, 78, 90, 88]. total = 0. for score in scores: total += score. average = total / len(scores). print(f'Average: {average:.1f}'). Output: Average: 86.6. The loop runs 5 times; after each, total accumulates: 85 → 177 → 255 → 345 → 433. Divide by 5: 86.6.
Example 2: Finding first even number with break: numbers = [3, 7, 12, 5, 8]. for n in numbers: if n % 2 == 0: print(f'First even: {n}'); break. Output: First even: 12. The loop checks 3 (odd, skip), 7 (odd, skip), 12 (even, print, break). The loop exits after 12; 5 and 8 are never checked.

Common mistakes

Self-check

Try each one before you look. A miss here costs nothing and tells you exactly what to reread.

1. How many iterations does `for i in range(3, 8):` produce?
2. What is the value of total after: total = 0; for i in range(1, 5): total += i
3. A while loop runs forever. What is the most likely cause?
4. What does the break keyword do inside a loop?
5. Which pattern correctly sums all even numbers from 1 to 10?

Canvas is the official record. This companion enhances the PGCC curriculum; it does not replace it. Last name and class year only. Students with a 504 plan or IEP: your accommodations apply.

← Selection StructuresFunctions and Modularity →

↑ Back to top