8. Control Structures

The algorithms of simple programs may consist of a series of actions stepped through sequentially. As you discovered in your design work, however, it is frequently necessary to choose between paths to take or to repeat steps a number of times. Without these options, it would be difficult to write programs of any complexity.

Control structures provide programmers with the means for determining (i.e., controlling) the order in which the steps in our programs are followed. There are three types of control structures: sequence, selection, and repetition. Each of these has been discussed in design and the implementation of selection and repetition control structures is presented in the sections which follow.

NameDescriptionC++ Implementation
SequenceSeries of statements which follow one after another.Default when other control structures are not used.
Selection
(also known as Decision)
Enable a choice to be made between statements to execute.

if statements

switch statements

Repetition
(also known as Iteration or Loop)
Enable execution of statements to repeat.

do loops

while loops

for loops

C++ allows any control structure to be nested completely within (i.e., placed inside of) another control structure. For example, a switch statement can be placed inside a do loop or a while loop placed within an if statement. A for loop can also be placed inside another for loop. Feel free to nest control structures as your programs require.