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
). Each program combines as many of each of these control statements as appropriate for the algorithm the program implements. We can model each control statement as an activity diagram with initial and final states representing that control statement’s entry and exit points, respectively. These single-entry/single-exit control statements make it easy to build programs—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’s only one other way to connect control statements—called control-statement nesting, in which one control statement is contained inside another.
Software Engineering Observation 4.1
Any C++ program can be constructed from only seven different types of control statements (sequence, if, if...else, switch, while, do...while and for) combined in only two ways (control-statement stacking and control-statement nesting).