4.4 Control Structures

Normally, statements in a program are executed one after the other in the order in which they’re written. This process is called sequential execution. Various C++ statements, which we’ll soon discuss, enable you to specify that the next statement to execute is not necessarily the next one in sequence. This is called transfer of control.

During the 1960s, it became clear that the indiscriminate use of transfers of control was the root of much difficulty experienced by software development groups. The blame was pointed at the goto statement (used in most programming languages of the time), which allows you to specify a transfer of control to one of a wide range of destinations in a program.

The research of Bohm and Jacopini1 had demonstrated that programs could be written without any goto statements. The challenge for programmers of the era was to shift their styles to “goto-less programming.” The term structured programming became almost synonymous with “goto elimination.” Not until the 1970s did most programmers start taking structured programming seriously. The results were impressive. Software development groups reported shorter development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects. The key to these successes was that structured programs were clearer, easier to debug and modify, and more likely to be bug free in the first place.

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures—the sequence structure, the selection structure and the iteration structure. We’ll discuss how each of these is implemented in C++.

4.4.1 Sequence Structure

The sequence structure is built into C++. Unless directed otherwise, the computer executes C++ statements one after the other in the order in which they’re written—that is, in sequence. The UML activity diagram in Fig. 4.2 illustrates a typical sequence structure in which two calculations are performed in order. C++ lets you have as many actions as you want in a sequence structure. As we’ll soon see, anywhere a single action may be placed, we may place several actions in sequence.

Fig. 4.2 Sequence-structure activity diagram.

An activity diagram models the workflow (also called the activity) of a portion of a software system. Such workflows may include a portion of an algorithm, like the sequence structure in Fig. 4.2. Activity diagrams are composed of symbols, such as action-state symbols (rectangles with their left and right sides replaced with outward arcs), diamonds and small circles. These symbols are connected by transition arrows, which represent the flow of the activity—that is, the order in which the actions should occur.

Like pseudocode, activity diagrams help you develop and represent algorithms. Activity diagrams clearly show how control structures operate. We use the UML in this chapter and Chapter 5 to show the flow of control in control statements. Online Chapters 25–26 use the UML in a real-world ATM (automated-teller-machine) case study.

Consider the sequence-structure activity diagram in Fig. 4.2. It contains two action states, each containing an action expression—for example, “add grade to total” or “add 1 to counter”—that specifies a particular action to perform. The arrows in the activity diagram represent transitions, which indicate the order in which the actions represented by the action states occur. The program that implements the activities illustrated in Fig. 4.2 first adds grade to total, then adds 1 to counter.

The solid circle at the top of the activity diagram represents the initial state—the beginning of the workflow before the program performs the modeled actions. The solid circle surrounded by a hollow circle at the bottom of the diagram represents the final state—the end of the workflow after the program performs its actions.

Figure 4.2 also includes rectangles with the upper-right corners folded over. These are UML notes (like comments in C++)—explanatory remarks that describe the purpose of symbols in the diagram. Figure 4.2 uses UML notes to show the C++ code associated with each action state. A dotted line connects each note with the element it describes. Activity diagrams normally do not show the C++ code that implements the activity. We do this here to illustrate how the diagram relates to C++ code. For more information on the UML, see our optional online object-oriented design case study (Chapters 25–26) or visit www.uml.org.

4.4.2 Selection Statements

C++ has three types of selection statements. The if statement performs (selects) an action (or group of actions), if a condition is true, or skips it, if the condition is false. The ifelse statement performs an action (or group of actions) if a condition is true and performs a different action (or group of actions) if the condition is false. The switch statement (Chapter 5) performs one of many different actions (or group of actions), depending on the value of an expression.

The if statement is called a single-selection statement because it selects or ignores a single action (or group of actions). The ifelse statement is called a double-selection statement because it selects between two different actions (or groups of actions). The switch statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).

4.4.3 Iteration Statements

C++ provides four iteration statements (sometimes called repetition statements or looping statements) that enable programs to perform statements repeatedly as long as a condition (called the loop-continuation condition) remains true. The iteration statements are the while, dowhile, for and range-based for statements. (Chapter 5 presents the dowhile and for statements and Chapter 7 presents the range-based for statement.) The while and for statements perform the action (or group of actions) in their bodies zero or more times—if the loop-continuation condition is initially false, the action (or group of actions) will not execute. The dowhile statement performs the action (or group of actions) in its body one or more times.

Each of the words if, else, switch, while, do and for are C++ keywords. Keywords cannot be used as identifiers, such as variable names, and must be spelled with only lowercase letters. Figure 4.3 provides a complete list of C++ keywords.

Fig. 4.3 C++ keywords.

C++ Keywords
Keywords common to the C and C++ programming languages
asm auto break case char
const continue default do double
else enum extern float for
goto if inline int long
register return short signed sizeof
static struct switch typedef union
unsigned void volatile while  
C++-only keywords
and and_eq bitand bitor bool
catch class compl const_cast delete
dynamic_cast explicit export false friend
mutable namespace new not not_eq
operator or or_eq private protected
public reinterpret_cast static_cast template this
throw true try typeid typename
using virtual wchar_t xor xor_eq
C++11 keywords
alignas alignof char16_t char32_t constexpr
decltype noexcept nullptr static_assert thread_local

4.4.4 Summary of Control Statements

C++ has only three kinds of control structures, which from this point forward we refer to as control statements: the sequence statement, selection statements (three types) and iteration statements (four types). Every program is formed by combining as many of these statements as is appropriate for the algorithm the program implements. We can model each control statement as an activity diagram. Like Fig. 4.2, each diagram contains an initial state and a final state that represent a control statement’s entry point and exit point, respectively. Single-entry/single-exit control statements make it easy to build programs— we simply connect the exit point of one to the entry point of the next. We call this control-statement stacking. We’ll learn that there’s only one other way in which control statements may be connected—control-statement nesting—in which one control statement appears inside another. Thus, algorithms in C++ programs are constructed from only three kinds of control statements, combined in only two ways. This is the essence of simplicity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset