Chapter 6 Summary and Quiz
Recap of loop forms and loop control.
Control Flow, Complete
Sequence, selection, repetition: with this chapter the triad is whole, and everything left in C is data and organization. You can now write any program shape the language supports; the coming chapters give you richer things to loop over. The recap.
The Three Loops
while tests before each pass and may run zero times. Its discipline: something in the body must move the condition toward false. Its signature shape is the sentinel loop, while (scanf("%d", &v) == 1 && v != 0), reading until a marker with short-circuit order guaranteeing the sentinel test only sees values actually read, and ending safely on bad input. while ((ch = getchar()) != EOF) is the same shape with end-of-input as sentinel.
do-while tests after the body: at least one pass, always, with the mandatory semicolon after the condition. Menus, retries, digit counting, wherever "at least once" is a fact of the problem.
for folds init, test, and update into one header: init once, test before each pass, body, update, test again. ANSI C declares the counter before the loop; after the loop it holds the first failing value. for (;;) loops forever; the comma operator legitimately runs paired updates. Known count: for. Discovered end: while. At least once: do-while.
Steering
break exits the innermost loop immediately; paired with while (1) it serves loops whose exit appears mid-pass. continue skips to the next pass, and in a for the update still runs first. Nested loops multiply: outer rows, inner columns, inner limits free to depend on outer counters (col <= row makes triangles), n by n means n squared passes. break escapes one level; escaping both takes a flag, restructuring, or the goto carve-out.
The Planted Bugs, Collected
The chapter's traps in one line each: a semicolon after a loop header makes an empty body; an update that fights the condition never ends; while (i = 10) assigns; shared counters between nested loops collide; and a continue in a while can jump the advance statement.
One Program, Whole Chapter
A sentinel while with a filtering continue, then a counting for that draws the answer: feed it 3 -8 7 2 0 and predict both lines (count includes the negative; the bar shows the biggest non-negative). Then take the quiz.
Looking Ahead
Chapter 7 gives loops something worthy of them: arrays, contiguous runs of memory where for (i = 0; i < n; i++) finally meets the data layout it was born to walk, and where the classic searching and sorting algorithms live.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Chapter 6 Summary and Quiz - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!