|
| C++ Program Design, 3/e James P. Cohoon,
University of Virginia Jack W. Davidson,
University of Virginia
Control constructs
Chapter 4 OverviewUp to this point, our programs—whether defined in a function main() or an
ApiMain()—have had the property that each time they are run, the exact same
sequence of statements is executed. Execution begins with the first statement in
the function and proceeds in a straight-line manner to the last statement in the
function with every statement along the way being executed once. This form of
programming is adequate for solving simple problems. However, for general
problem solving we need the ability to control which statements are executed
and how often. In this chapter, we consider two conditional constructs—the if
and switch—that control whether a statement list is executed and three iterative
constructs—the while, for, and do—that control how many times a statement
list is executed. Except for the switch construct, which performs a
matching process to determine which statements are executed, these control
constructs use logical expressions to determine their course of action. To sup-port
these constructs, C++ has the logical type bool. Our control construct
examination begins with logical expressions and the type bool. |
|
|