while, do-while, for), the two flow control statements (break and continue), and the three patterns that come up over and over in real code: counting, accumulating, and searching. The Loop Tracer in Section 5 lets you step one iteration at a time and watch variables change. The lab applies the same patterns to an aviation fuel log.
By the end of this lesson, cadets will be able to:
while, do-while, and for loops. (Outcome 1)break and continue to control loop flow. (Outcome 1)Complete before class. These set the vocabulary we will use.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 3 (the loop sections: while, do-while, for) | All three loop forms with C syntax. |
| King, C Programming: A Modern Approach | Chapter 6 (Loops) | Optional deeper dive with more worked examples and pattern catalog. |
while loop and the difference from do-whilefor loop and its three parts (init, condition, update)break and continueLesson 4 taught your programs to decide. Lesson 5 teaches them to decide again and again. Every loop in C is built from the same three parts: an initial state, a condition that says when to keep going, and an update that moves the state forward. The three loop forms just rearrange those parts. Once that clicks, the bugs become predictable and the right loop form for any job becomes obvious.
The Lesson 5 deck walks through the three loop forms, the two flow control statements, the three patterns, and the four common bugs. The deck is the spine of the in class lecture; the Loop Tracer is where you watch every concept play out one step at a time.
A single page reference for finding wrong output bugs in your loops. Keep it open in another tab while you work on Lab 05. Real debugger and valgrind training arrives at Lesson 16, for bugs the source code does not make visible.
Open print-debugging reference card →
Every loop you will ever write has three parts. Identifying them up front is the fastest way to debug a loop that misbehaves.
All three loop forms (while, do-while, for) have those three parts. They differ only in where the parts go and when the condition gets checked.
A while loop checks its condition at the top of each iteration. If the condition is false the first time, the body never runs at all.
Use while when you do not know in advance how many iterations you need: reading until end of input, retrying until success, simulating until a condition is reached.
A do-while loop runs the body once before checking the condition. The body always runs at least once.
Use do-while when the body has to run at least once before you have anything to check. The classic case is input validation: you cannot check whether the user typed a valid number until they have typed something.
Note the semicolon after the while (...) at the end. It is required, and forgetting it is a common compile error.
A for loop puts initialization, condition, and update on the same line. The body is just the body. This is C's loop of choice when you know how many iterations you want.
Part of for (a; b; c) |
When it runs |
|---|---|
| a (init) | Once, before the first iteration. |
| b (condition) | Before each iteration (including the first). If false, the loop exits. |
| c (update) | After each iteration, before the next condition check. |
The variable declared in the init part (here int i) is scoped to the loop. It does not exist after the loop ends. This is what you want; it makes scoping bugs harder.
break exits the loop immediately. Control jumps to the first statement after the loop's closing brace. Use it when you have found what you were looking for and want to stop early.
continue skips the rest of the current iteration and jumps to the next iteration's condition check. The loop keeps running; only this trip through the body is cut short.
break and continue hide control flow. A reader has to look inside the body to know when the loop exits, instead of just reading the condition. Use them when they make the code clearer (early-exit searches, skip-the-bad-cases filters) but do not reach for them as the first solution.Almost every loop you write in CS210 fits one of three patterns. Identifying which pattern you need is the first design decision.
| Pattern | What it tracks | Typical loop form |
|---|---|---|
| Counting | A counter (how many times). Print a table, loop N times, walk an array of known size. | for (the count goes in the header). |
| Accumulating | A running total or running max / min. Initialize the accumulator before the loop. | for over a known range or while while-more-input. |
| Searching | A flag or index that says "found it." Set a sentinel value before the loop; update and break on the first match. |
for with an early break. |
The lab tonight uses all three: a do-while to validate input, a counting for to read N flight entries, and accumulators tracking total fuel, total hours, and the maximum burn flight.
i < 5 but you wanted the loop to include 5. Or you wrote i <= n when iterating an array of size n. The body runs one too many or one too few times.int sum; to 0 before the loop. C does not zero locals for you; sum holds whatever bits happened to be there. Output is a huge garbage number, and worse, it changes between runs.int sum = 0; Never int sum; when you mean zero.for (int i = 0; i < 5; i++); with a semicolon right after the parentheses. That semicolon is the loop body. Your real body (the next block) runs exactly once with i = 5 after the empty loop finishes. The compiler does not warn you.for (...) or while (...). The opening brace goes there.Loops introduce five rules from the DFCS C Programming Standard. The standard is the course style guide; you can find the full text in the Resources section.
++ or -- inside a complex expression. Increment on its own line.break in loops. (Inside switch is fine and expected.) Most search loops can be rewritten with a flag in the condition.continue. Wrap the work in an if instead.goto.The two that need the most care: §7.6 and §7.7. break and continue work, you will see them in real code, and you need to read them. The standard is not zero tolerance. It is no overuse. Reach for the cleaner alternative first; use the keyword only when it is genuinely clearer than the rewrite.
Step through each round. Watch the loop variable change, watch the accumulator grow, watch break and continue redirect the flow. The state pane on the right tracks everything.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.
Read a flight log from stdin and report summary statistics. Each entry is one flight: hours flown and gallons burned. You decide which loop forms to use and which loop patterns each summary needs.
Open the README in your repo for the full spec, the input and output format, the exact strings to print, and the test cases.