April 5, 2008
WikiAlgorithms
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: \
- the actions to execute and \
- 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. \
- C++ provides three types of selection statements. 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 performs one of many different actions, depending on the value of an integer expression.
Selection Statements in C++
- The if selection statement is a single-selection statement because it selects or ignores a single action (or, as we will 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 (also called looping statements or loops) that enable programs to perform statements repeatedly as long as a condition (called the loop-continuation condition) remains true. The repetition statements are the while, do…while and for statements. The while and for statements perform the action (or group of actions) in their bodies zero or more timesif 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.\
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.
Continue Reading
Back to Archive