Iteration and Loops
Outcome 4: Iteration and Loops · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx
Objectives
- Write for loops that iterate over range() and over lists.
- Write while loops with conditions that eventually become False.
- Use break to exit a loop early and continue to skip an iteration.
- Apply the accumulator pattern to compute sums, products, and counts.
- Identify and fix infinite loop conditions.
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
Common mistakes
- Off-by-one errors with range. range(n) gives 0 to n-1 — if you want 1 to n, use range(1, n+1).
- Infinite while loop — forgetting to update the condition variable. while count < 10: print(count) never terminates because count is never changed. Add count += 1 inside the loop.
- Modifying a list while iterating over it. This can skip elements or cause unexpected behavior. Iterate over a copy if you need to remove items: for item in list.copy(): ...
Self-check
Try each one before you look. A miss here costs nothing and tells you exactly what to reread.
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.