Iteration

4. Iteration#

Processing a range of values or repeatedly asking for valid input requires controlled repetition. This chapter connects loop initialization, continuation conditions, and updates to counted work, string traversal, and running totals. You will explain why a loop stops and test its first, last, and empty cases; the GCD lesson provides additional supporting practice.

  • Loop control: A loop needs a starting state, a continuation condition, and an update that moves it toward stopping.

  • for loops: A for loop works well when the number of repetitions or index range is known.

  • while loops: A while loop works well when repetition depends on a condition that may change during execution.

  • Loop safety: Careful updates and stopping conditions prevent infinite loops and off-by-one errors.

Chapter Video

C# for loops 🔁 (3:48) by Bro Code.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Write counted loops with for and open-ended loops with while.

  2. Trace loop variables through repeated iterations, including steps that cross a boundary.

  3. Use loops to process strings, numeric ranges, and repeated prompts.

  4. Use break and continue only when they clarify loop behavior.

  5. Recognize and avoid infinite loops and off-by-one mistakes.

Chapter Flow

Glossary

#

Term

Explanation

1

Iteration

Repeating a block of code while a pattern or condition requires it.

2

Loop

A programming structure that repeats statements.

3

For loop

A loop commonly used when the number of repetitions is known or counted.

4

While loop

A loop that continues while a Boolean condition remains true.

5

Do-while loop

A loop that runs its body once before checking the condition.

6

Foreach loop

A loop that visits each item in a collection.

7

Loop variable

A variable used to track loop progress.

8

Condition

The Boolean test that controls whether a loop continues.

9

Accumulator

A variable that gathers a result across loop iterations.

10

Sentinel value

A special input value that tells a loop to stop.

11

Break

A statement that exits a loop immediately.

12

Continue

A statement that skips to the next loop iteration.

13

Infinite loop

A loop that continues indefinitely because execution never reaches an exit.

14

Off-by-one error

A loop boundary mistake that runs one too many or one too few times.

15

Sentinel

A designated input value that ends a repeated-input process.

16

Counter

A variable recording how many events or qualifying items have occurred.

17

Progress measure

A quantity whose change explains why repetition approaches an exit.

18

Loop invariant

A relationship preserved across complete loop iterations.

19

Empty input

A sequence with no data items to process, which may require zero body executions.

20

Step size

The amount a counter changes after a loop pass.

21

Inclusive upper bound

A limit that permits a generated value equal to the bound.

Lecture Slides

Chapter 4 lecture slides follow the current reading order. Supporting parts remain optional; instructor notes and editable decks are distributed separately.