Table of Contents

Algorithms

Any solvable computing problem can be solved by the execution of a series of actions in a specific order. A procedure for solving a problem in terms of:

  1. the actions to execute and
  2. the order in which these actions execute

is called an algorithm. The following example demonstrates that correctly specifying the order in which the actions execute is important.

Consider the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work: (1) Get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast, (6) carpool to work. This routine gets the executive to work well prepared to make critical decisions. Suppose that the same steps are performed in a slightly different order: (1) Get out of bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work. In this case, our junior executive shows up for work soaking wet. Specifying the order in which statements (actions) execute in a computer program is called program control.

Böhm and Jacopini’s work 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 will refer to them in the terminology of the C++ standard document as “control statements.”

Sequence Structure in C++

* 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 are writtenthat is, in sequence.

Selection Statements in C++

Repetition Statements in C++

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 typesif, if...else and switch) and repetition statements (three typeswhile, for and do...while). Each C++ program combines as many of these control statements as is appropriate for the algorithm the program implements. As with the sequence statement, 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 make it easy to build programs. the control statements are attached to one another by connecting the exit point of one to the entry point of the next. This is similar to the way a child stacks building blocks, so we call this control-statement stacking. We will learn shortly that there is only one other way to connect control statements called control-statement nesting, in which one control statement is contained 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.