# Loops and Repeated Computation

Computer Science I · Programming I · https://tryals.app/learn/computer-science-i/loops-and-repeated-computation

## Doing It Again

A **loop** repeats a block of code. The staple forms are the **while loop** and the **for loop**.

A **while loop** runs as long as a condition stays true, checking it before each pass. Correct execution requires three ingredients:

| Ingredient | Role in Code | Example |
| --- | --- | --- |
| **Initialization** | Sets start point | `i = 0` |
| **Condition** | Decides when to stop | `i < 5` |
| **Update** | Moves toward false | `i = i + 1` |

Forget the update, and you get an **infinite loop** that freezes the program by running forever.

A **for loop** handles known repetitions or steps through collections. For example, $range(n)$ produces values starting at 0 and stopping before $n$:

$$\text{range}(n): 0, 1, \ldots, n-1$$

## Control and Pitfalls

The **for loop** convention stops before $n$, a frequent source of the **off-by-one error** where a loop runs one time too many or too few because a boundary like `<` versus `<=` is wrong.

Two control statements adjust loops internally:

| Statement | What It Does |
| --- | --- |
| `break` | Exits the loop immediately |
| `continue` | Skips to the next iteration |

Loops can also **nest**, placing one inside another. If an outer loop runs $n$ times and an inner loop runs $m$ times, the inner body executes $n \times m$ times, directly impacting performance.

## Practice questions

5 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Order the parts of a correct while loop so that it counts up and then stops.

**Answer:**

1. Initialize the counter: i = 0
2. Check the condition before the pass: is i < 5 ?
3. Run the body of the loop
4. Update the counter toward the condition becoming false: i = i + 1
5. Return to check the condition again

**Why:** Initialize, then test, run the body, update, and loop back to test again. Omitting the update is the classic infinite loop: the condition stays true forever because nothing moves the counter toward making it false.

Page: https://tryals.app/practice/computer-science-i/loops-and-repeated-computation/order-the-parts-of-a-correct-while-loop-so-that-it-counts-up-and-then

### 2. Match each loop keyword or form to what it does.

**Answer:**

- while → Repeats as long as a condition stays true
- for → Repeats a known number of times or over a collection
- break → Exits the loop immediately
- continue → Skips to the next iteration

**Why:** A while loop is condition-driven and a for loop is count- or collection-driven; break abandons the whole loop while continue merely ends the current pass and moves on. Confusing break with continue changes how much of the loop is skipped.

Page: https://tryals.app/practice/computer-science-i/loops-and-repeated-computation/match-each-loop-keyword-or-form-to-what-it-does

### 3. Modifying a list while looping over it can silently skip elements. Why does removing an item mid-iteration cause that?

A. Because Python caches the list's contents before the loop begins
B. Because the index advances while the list shifts left beneath it
C. Because the list becomes invalid once modified, and the loop reads garbage
D. Because removal is performed after the loop finishes

**Answer:** B. Because the index advances while the list shifts left beneath it

**Why:** The list shifts left and the index moves right, so each removal skips the element that slid into place, remove every item from [a,b,c,d] and you visit a and c. The fixes are to iterate over a copy or build a new list, and it is the standard reason 'why did my filter miss half the entries'.

Page: https://tryals.app/practice/computer-science-i/loops-and-repeated-computation/modifying-a-list-while-looping-over-it-can-silently-skip-elements

### 4. The counter starts at i = 0. Select every loop below that runs its body exactly 5 times.

A. for i in range(1, 6)
B. while i < 5 (with i = i + 1 each pass)
C. while i <= 5 (with i = i + 1 each pass)
D. while i < 4 (with i = i + 1 each pass)
E. for i in range(5)

**Answer:** A. for i in range(1, 6); B. while i < 5 (with i = i + 1 each pass); E. for i in range(5)

**Why:** range(5) gives 0..4 and while i < 5 from 0 gives 0..4, both 5 passes; range(1, 6) gives 1..5, also 5. But while i <= 5 runs 0..5, which is 6 passes, and while i < 4 runs 0..3, only 4. The <= versus < boundary is the exact off-by-one to watch.

Page: https://tryals.app/practice/computer-science-i/loops-and-repeated-computation/the-counter-starts-at-i-0-select-every-loop-below-that-runs-its

### 5. An off-by-one error in a loop boundary usually causes a crash, which is what makes it easy to find.

**Answer:** False

**Why:** False. Off-by-one errors usually produce a *wrong result* — one item skipped, one processed twice — with no crash at all, which is exactly what makes them hard to find. They surface as a total that is slightly off, and only an out-of-range index turns one into an exception.

Page: https://tryals.app/practice/computer-science-i/loops-and-repeated-computation/an-off-by-one-error-in-a-loop-boundary-usually-causes-a-crash-which
