C For Loop
Visit a counted range with for, keep the bound exclusive or inclusive on purpose, and prove the loop can stop.
Use for when the count is visible from the start
A for loop places its three moving parts together: initialization runs once, the condition is checked before every cycle, and the update runs after the body. This structure fits numbered lessons, days, retries with a fixed limit, and array indexes. Declaring the index in the for header (for (int day = 1; ...)) is valid C99 and later, which this course uses.
Trace a counted practice loop
The loop starts day at 1, runs while day is at most total_days, then changes day with day++.
Edit the program, predict its output, then run it.
- 01Initialize once
int day = 1creates the starting index before the first condition check. - 02Test before the body
day <= total_daysdecides whether another cycle is allowed. - 03Run one cycle
The body uses the current index.
- 04Update toward the stop
day++adds one so the condition can become false.
Trace the first, last, and forbidden values
Most loop bugs are boundary bugs. Agree on whether the index is zero-based or one-based and whether the ending value belongs in the range. The starter below prints four lines for three lessons: it starts at 0 and includes the endpoint 3. Repair it by starting at 1 with <= total_lessons, or starting at 0 with lesson < total_lessons.
Repair an off-by-one loop
Change one part of the loop header, predict the output, then run it again.
Edit the program, predict its output, then run it.
Let the inner loop finish before the outer loop advances
With weeks = 2 and days = 3, expect six lines. Give each index a meaningful name and keep the body small. Nested loops that search every pair of a large collection need a better algorithm later; here the job is to trace the total work.
Trace a two-level practice schedule
Notice that the day counter resets to 1 each time a new week begins.
Edit the program, predict its output, then run it.
Use break and continue sparingly
break leaves the nearest loop. continue skips the rest of this cycle; a for loop still performs its update before testing again. A plain loop condition is still the clearest normal stopping rule.
Skip one cycle and stop after a found result
Attempt 2 uses continue. Attempt 4 uses break.
Edit the program, predict its output, then run it.
Arrays use index < count
When you visit every element of an array with count items, the usual header is for (int index = 0; index < count; index++). The array does not store its own length. Details belong in C Arrays. Prefer C While Loop when the stop depends on changing state rather than a known count.
Independent lab: print a numbered list
Print one labeled line for each of 1 through n, then change n and predict the last printed number and the value of the index after the loop ends.
Lesson review
- A
forheader names initialization, condition, and update. - Boundary tests use the first value, last allowed value, cycle count, and the value after the last update.
- An inner loop completes for one outer value before the outer index changes.
breakandcontinueare exceptional exits, not a substitute for a clear condition.