Chapter 5 Summary and Quiz
Recap of branching constructs.
Programs That Decide
The straight line is broken: your programs now validate their input, choose among ranges, dispatch on values, and even know the one disciplined use of the jump everyone warns about. This chapter's material is compact but load-bearing; every later chapter assumes it. The recap.
The if Family
if (condition) runs its branch when the condition is non-zero; else covers the rest. Braces go on every branch, because an unbraced else binds to the nearest unmatched if, whatever the indentation says, and unbraced "second statements" sit outside the if that visually owns them. if (x = 100) assigns and is always true; == compares; the compiler's warning about it is always right.
The else-if ladder runs exactly one branch, first true condition wins, so ranges are ordered from the top rung down and a bare else nets everything below. Misordered rungs make later grades unreachable, a standard planted bug.
And the pattern now standard in every course program: guard the read.
if (scanf("%d", &marks) != 1) {
printf("invalid input\n");
return 1;
}
The return value finally has a consequence: report, and exit non-zero, the operating system's word for failure.
switch
switch (integerExpression) jumps to the matching constant label; default catches the rest. Case labels are entry points, not walls: without break, execution falls through into the next case's statements. Trace exam switches as straight-line code from the entry label, stopping only at break or the closing brace. Stacked empty labels over one body are the idiom for "these values are alike"; fallthrough past statements is a bug unless loudly commented. Ranges, doubles, and compound conditions stay with the ladder; fixed integer alternatives read best as a switch.
goto
An unconditional jump to a function-scoped label. Structured statements replaced it (Dijkstra, 1968, "Go To Statement Considered Harmful"), and every ordinary use has a structured rewrite. The one respectable survivor is the forward jump to shared error cleanup, which returns with real resources in the dynamic-memory and file chapters. Exam calibration: define, cite, concede the cleanup exception.
One Program, Whole Chapter
A guarded read, a ladder with an early error exit, and a switch with stacked labels: feed it 2 150 and predict both lines before running. Then take the quiz.
Looking Ahead
Chapter 6 adds the last control-flow pillar: loops. while, do-while, and for turn one decision into many, and the read-until-EOF shape previewed in chapter 4 finally becomes yours.
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 5 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!