- 4.1 Introduction
- 4.2 Control Structures
- 4.3 if Selection Statement
- 4.4 if...else Double-Selection Statement
- 4.5 while Repetition Statement
- 4.6 Counter-Controlled Repetition
- 4.7 Sentinel-Controlled Repetition
- 4.8 Nested Control Statements
- 4.9 Assignment Operators
- 4.10 Increment and Decrement Operators
- 4.11 (Optional) Software Engineering Case Study: Identifying Class Attributes in the ATM System
- 4.12 Wrap-Up
4.2 Control Structures
Böhm and Jacopini's research1 demonstrated that all programs could be written in terms of only three control structures, namely, the sequence structure, the selection structure and the repetition structure. The term "control structures" comes from the field of computer science. When we introduce C++'s implementations of control structures, we'll refer to them in the terminology of the C++ standard document2 as "control statements."
Sequence Structure in C++
The sequence structure is built into C++. Unless directed otherwise, C++ statements execute one after the other in the order in which they are written—that is, in sequence. The Unified Modeling Language (UML) activity diagram of Fig. 4.1 illustrates a typical sequence structure in which two calculations are performed in order. C++ allows us to have as many actions as we 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.1 Sequence-structure activity diagram.
In this figure, the two statements involve adding a grade to a total variable and adding 1 to a counter variable. Such statements might appear in a program that averages several student grades. To calculate an average, the total of the grades is divided by the number of grades. A counter variable would be used to keep track of the number of values being averaged. You'll see similar statements in the program of Section 4.6.
Activity diagrams are part of the UML. 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, such as the sequence structure in Fig. 4.1. Activity diagrams are composed of special-purpose symbols, such as action state symbols (a rectangle with its left and right sides replaced with arcs curving outward), diamonds and small circles; these symbols are connected by transition arrows, which represent the flow of the activity. Activity diagrams help you develop and represent algorithms. As you'll see, activity diagrams clearly show how control structures operate.
Consider the sequence-structure activity diagram of Fig. 4.1. It contains two action states that represent actions to perform. Each action state contains an action expression— e.g., "add grade to total" or "add 1 to counter"—that specifies a particular action to perform. Other actions might include calculations or input/output operations. The arrows in the activity diagram are called transition arrows. These arrows represent transitions, which indicate the order in which the actions represented by the action states occur—the program that implements the activities illustrated by the activity diagram in Fig. 4.1 first adds grade to total, then adds 1 to counter.
The solid circle located at the top of the activity diagram represents the activity's initial state—the beginning of the workflow before the program performs the modeled activities. The solid circle surrounded by a hollow circle that appears at the bottom of the activity diagram represents the final state—the end of the workflow after the program performs its activities.
Figure 4.1 also includes rectangles with the upper-right corners folded over. These are called notes in the UML. Notes are explanatory remarks that describe the purpose of symbols in the diagram. Notes can be used in any UML diagram—not just activity diagrams. Figure 4.1 uses UML notes to show the C++ code associated with each action state in the activity diagram. A dotted line connects each note with the element that the note describes. Activity diagrams normally do not show the C++ code that implements the activity. We use notes for this purpose here to illustrate how the diagram relates to C++ code. For more information on the UML, see our optional case study, which appears in the Software Engineering Case Study sections at the ends of Chapters 1–7, 9 and 13, or visit www.uml.org.
Selection Statements in C++
C++ provides three types of selection statements (discussed in this chapter and Chapter 5). The if selection statement either performs (selects) an action if a condition (predicate) is true or skips the action if the condition is false. The if...else selection statement performs an action if a condition is true or performs a different action if the condition is false. The switch selection statement (Chapter 5) performs one of many different actions, depending on the value of an integer expression.
The if selection statement is a single-selection statement because it selects or ignores a single action (or, as we'll soon see, a single group of actions). The if...else statement is called a double-selection statement because it selects between two different actions (or groups of actions). The switch selection statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).
Repetition Statements in C++
C++ provides three types of repetition statements that enable programs to perform statements repeatedly as long as a condition remains true. The repetition statements are the while, do...while and for statements. (Chapter 5 presents the do...while and for statements.) 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 do...while statement performs the action (or group of actions) in its body at least once.
Each of the words if, else, switch, while, do and for is a C++ keyword. These words are reserved by the C++ programming language to implement various features, such as C++'s control statements. Keywords must not be used as identifiers, such as variable names. Figure 4.2 provides a complete list of C++ keywords.
Fig. 4.2 C++ keywords.
C++ Keywords |
||||
Keywords common to the C and C++ programming languages |
||||
auto |
break |
case |
char |
const |
continue |
default |
do |
double |
else |
enum |
extern |
float |
for |
goto |
if |
int |
long |
register |
return |
short |
signed |
sizeof |
static |
struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
while |
|||
C++-only keywords |
||||
and |
and_eq |
asm |
bitand |
bitor |
bool |
catch |
class |
compl |
const_cast |
delete |
dynamic_cast |
explicit |
export |
false |
friend |
inline |
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 |
Summary of Control Statements in C++
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—if, if...else and switch) and repetition statements (three types—while, for and do...while). As with the sequence statement of Fig. 4.1, we can model each control statement as an activity diagram. Each diagram contains an initial state and a final state, which represent a control statement's entry point and exit point, respectively. These single-entry/ single-exit control statements are attached to one another by connecting the exit point of one to the entry point of the next. We call this control-statement stacking. There is only one other way to connect control statements—called control-statement nesting, in which one control statement is contained inside another.