In our previous lessons we have seen some simple programs, and you have even written some programs to perform basic calculations. These programs have illustrated a very simple flow of control in a program. The program flow of control is the order in which the computer executes the statements in a program. For the simple tax program, the flow of control is sequential, that is, each statement is executed one by one in order from top to bottom. This idea of sequential flow of control is shown below with a down arrow.
|Flow of Control||
Simple Tax Program
:= Price * TAXRATE
In many programming languages, the sequential flow of control is the default behavior. However, we often need to alter this flow when we write programs. To do this, most programming languages provide at least three control structures for altering the sequential flow of control. These control structures are known as selection, loop, and subprogram. Combining these control structures with the default sequential flow of control, we have four ways of specifying the flow of control in a program. In the next few lessons we will look at each of the three control structures in more detail. For now, we will briefly introduce them in the table below on control flow.
Program Flow of Control
Sequence is composed of a series of statements which are executed one by one from top to bottom. Sequence is the default flow of control for many programming languages. All of the programs illustrated so far have used this flow of control for their execution.
|Selection is used to alter the flow of control when a choice needs to be made between two or more actions. Often the choice is based on the state of some variables in the program. This control structure is commonly specified using the keywords If and Else.|
|Loop is a control structure that causes a set of statements to be executed repeatedly. With each loop iteration, a test is performed to determine whether the loop should continue or end. Often this control structure is specified using the key word While.|
|Subprograms are a way of grouping statements that provide a single logical operation. An example subprogram might be SquareRoot which could find the square root of a number and return the result to the main program. The keyword Call indicates a subprogram.|