Chapter 3: Everything is about control
This introduction contrasts unconditional function calls and returns with conditional control statements, then identifies the roles of C’s five conditional statements.
After this chapter you will be able to
- Unconditional and conditional control. Distinguish the unconditional transfer of control in a function call or return from the condition that governs whether a for loop’s block executes.
- Five conditional control statements. Describe the roles assigned here to if, for, do, while, and switch.
- Conditional code path selection. Explain how an if statement uses its controlling expression to decide whether to execute a secondary block, and how else supplies the alternative path.
- Scalar conditions and truth values. Explain how C interprets a numerical value as a condition and state that scalar types, including numerical and pointer types, have truth values.
- For loop parts and scope. Explain how a for statement initializes an iteration variable, tests whether to continue, and updates the variable at the end of each iteration. Describe why separate for loops can each declare a variable named i.
- While and do condition timing. Distinguish while from do by when each tests its condition and what happens if that condition is false initially.
- Break and continue effects. Distinguish how break and continue affect execution of the remaining loop body and whether the loop carries on.
- Switch selection and default. Explain how a switch chooses a case from an expression's value and when the default case runs.
- Break and continued execution. Trace what happens after control reaches a case label, including the effects of break and omitting it.
What it builds on
- Iteration (chapter 2) · The introductory use of iteration.
- Function calls (chapter 2) · Function calls as statements.
- Function return (chapter 2) · Returning from a function.
- Statements (chapter 2) · Assumes familiarity with statements and blocks of code.
Section by section
Introduction
This introduction contrasts unconditional function calls and returns with conditional control statements, then identifies the roles of C’s five conditional statements.
- Unconditional and conditional control: Distinguish the unconditional transfer of control in a function call or return from the condition that governs whether a for loop’s block executes.
Why it matters: This distinction helps you describe why a function call and a loop change execution in different ways. - Five conditional control statements: Describe the roles assigned here to if, for, do, while, and switch.
Why it matters: These categories give you a starting point for recognizing what kind of control a statement provides.
Key terms: functions, call, return statement, for statement, controlling condition, conditional control statements, if, for, do, while
3.1 Conditional execution
This section explains how C’s if and else select code paths, how scalar values act as conditions, and how to write clear truth-value tests.
- Conditional code path selection: Explain how an if statement uses its controlling expression to decide whether to execute a secondary block, and how else supplies the alternative path.
Why it matters: This is the basic way to make execution depend on a condition. - Scalar conditions and truth values: Explain how C interprets a numerical value as a condition and state that scalar types, including numerical and pointer types, have truth values.
Why it matters: It explains why an if condition need not be an explicit comparison. - Equality tests and direct conditions: Distinguish == from != and explain why a condition can use a truth value directly, or its negation with !, instead of comparing it with 0, false, or true.
Why it matters: Direct conditions can avoid clutter without changing the intended truth test. - Boolean values in code: Describe the purpose of bool and explain why false and true are preferred over numerical literals when storing truth values.
Why it matters: Named truth values communicate the intended meaning of a stored value.
Key terms: if, else, controlling expression, secondary block, selection statement, code paths, scalar, truth value, logical false, logical true
3.2 Iterations
This section explains C’s three iteration statements, how break and continue alter a loop, and how loops are used in a program that computes multiplicative inverses.
- For loop parts and scope: Explain how a for statement initializes an iteration variable, tests whether to continue, and updates the variable at the end of each iteration. Describe why separate for loops can each declare a variable named i.
Why it matters: Reading each part of a for loop helps you trace its iterations and understand where its variables belong. - While and do condition timing: Distinguish while from do by when each tests its condition and what happens if that condition is false initially.
Why it matters: The test position determines whether a loop body can be skipped entirely. - Break and continue effects: Distinguish how break and continue affect execution of the remaining loop body and whether the loop carries on.
Why it matters: These statements let a loop stop early or skip work without executing the rest of its body.
Key terms: for statement, clause1, condition2, expression3, scope, while, do, condition, secondary block, break
3.3 Multiple selection
This section explains how switch selects among multiple cases, how execution continues after a case label, and what restrictions make switches safe to use.
- Switch selection and default: Explain how a switch chooses a case from an expression's value and when the default case runs.
Why it matters: It lets you describe a choice with several outcomes rather than a cascade of true-or-false decisions. - Break and continued execution: Trace what happens after control reaches a case label, including the effects of break and omitting it.
Why it matters: This explains both deliberate fall-through and unexpected execution of later cases. - Case value restrictions: State what expressions may label cases and why a variable cannot be used as a case value.
Why it matters: These restrictions determine which multiple-choice decisions can be written as a switch. - Skipped variable initialization: Explain how a switch jump can bypass a variable definition's initializer and make subsequent use erroneous.
Why it matters: A switch can enter a block at a point where needed initialization has not happened.
Key terms: switch statement, case, default, case labels, jump targets, break, integer constant expressions, case values, variable definitions, initializer
3.4 Summary
Where this is used later
- Simple functions (chapter 7) · The discussion of functions develops the control transfer introduced here.
- Comparison (chapter 4) · Earlier use of comparisons in control statements.
- Logic (chapter 4) · The rules for treating values as false or true in conditional execution.
- Boolean context (chapter 4) · Provides the truth-value rule used in Boolean contexts.
- Pointers as opaque types (chapter 6) · The section identifies pointers as scalar types with truth values.
- Boolean values (chapter 5) · Examines Boolean values in greater depth.
- (chapter intro) (chapter 19) · Assumes familiarity with loops as control-flow constructs.
- Initializers (chapter 5) · the later discussion examines initialization in more detail
Concepts explained in this chapter
- stopping criterionA condition is an expression whose evaluated value controls conditional or iterative execution in C.
- break statementA break statement terminates the nearest enclosing switch or iteration statement and transfers control to the following statement.
- loopA for statement is a C iteration statement that controls repeated execution using initialization, a continuation condition, and an iteration expression.
- do statementA do-while loop is a C iteration statement that executes its body once and then repeats it while its controlling condition is true.
- conditional logicAn if-else statement conditionally selects between alternative statements based on a controlling expression.
- Heron approximationThe Heron approximation is an iterative numerical method for refining an estimate of a reciprocal or square-root-related quantity.
- continue statementA continue statement skips the remaining body of the current loop iteration and proceeds with that loop's next iteration step.
- multiplicative inverseA multiplicative inverse of a nonzero number is a number whose product with it equals one.
- case statementA switch statement is a C selection statement that transfers control to a matching case or default label based on an expression's value.
- booleanbool is a C type whose values represent logical truth and falsity.
- truthinessA truth value is the logical interpretation of a C value as true or false in a condition or logical operation.
- integer constant expressionAn integer constant expression is a C expression whose value can be determined during translation using only permitted constant operands and operations.
- compound statementA compound statement is a C statement consisting of a brace-delimited sequence of statements treated as one statement.
- tgmath.htgmath is the C type-generic mathematics facility that provides interfaces selecting numerical functions according to argument types.
- comparisonA comparison operator is a C operator that evaluates a relational condition between two operands and produces a Boolean result.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
