header

Standard Control Structures

Control structures determine the order in which program statements are executed.

Sequential Structure

 In a sequential structure, the statements are executed one after the other in the order in which they appear in the program code. This is the most fundamental control structure.

Decision Structures

In a decision structure, a test condition determines which block of code (if any) is to be executed next. Decision structures involve conditional execution. A block of code is executed only if the right conditions exist. The conditions for execution are embodied in a Boolean (logical) expression that evaluates to either true or false. (See the logical expressions page for summary information.)

The If-Then Structure

The if-then structure chooses between two options: execute a block of code or skip it entirely. In an if-then structure, a logical expression (the condition) is evaluated and if it is true then the specified block of code is executed; otherwise it is not. In either case, execution resumes with the first statement following the specified block.

In the illustration below, there are two possible paths through the if-then control structure. If the condition evaluates to true, the green path is followed and the code in the if clause is executed. Otherwise, the red path is followed and that code is skipped. This structure represents a "go" or "no go" decision.

If Then Diagram

Example ◄Click to see an example of an if-then structure.

The If-Then-Else Structure

The if-then-else structure also chooses between two options. However, the nature of those options is different. In an if-then structure, the CPU executes a block of code or skips it. In an if-then-else structure, the choice is between two blocks of code. If the test condition is true then one block of code will be executed (the code in the if clause); otherwise a second block of code will be executed (the code in the else clause).

In the illustration below, there are two possible paths through the if-then-else control structure. If the condition evaluates to true, the green path is followed. Otherwise, the red path is followed. This structure represents a "do this" or "do that" decision.

If Then Else Diagram

Example ◄Click to see an example of an if-then-else structure.

The Nested If-Then-Else Structure

In a nested if-then-else structure, the if clause, the else clause, or both are themselves selection structures. Nesting can extend to as many levels as needed. Nested if-then-else structures are used to choose from among three or more options.

The Switch Statement

The switch statement is generally used as an alternative to a nested if-then-else statement though it has some unique properties.

Looping Structures

A looping structure is used to repeatedly execute the same block of code; typically with different data. The number of repetitions is controlled by a Boolean expression. There are three requirements for a properly written looping structure:

  1. A looping structure has at least one termination condition.
  2. Each iteration moves the state of the program closer to a termination condition.
  3. A termination condition can be reached in a finite (and reasonable) number of iterations.

The While Loop

The while loop is a pre-test loop; the condition is tested before the statements in the body of the loop are executed. While the test condition is true, execute the statements in the body of the loop. The loop terminates when the condition becomes false. Because the test precedes the body of the loop, the statements in the body will never be executed if the condition is false the very first time it is tested.

The only difference between the while loop and the if-then structure above is what happens after the conditional code has been executed. In the if-then structure, the program proceeds to the next instruction. In the while loop, the program goes back and retests the condition.

While Loop Diagram

Example ◄Click to see an example of a while loop.

The Do-While Loop

The do-while loop is a post-test loop; the condition is tested after the statements in the body of the loop have been executed. As long as the test condition is true, the program will go back and repeat the body of the loop. The loop terminates when this condition becomes false. Because the test follows the body of the loop, it (the body) will always be executed at least once.

Do While Loop Diagram

Example ◄Click to see an example of a do-while loop.

The For Loop

The for loop behaves like a while loop (it is a pre-test loop) but with syntax specialized for creating a counter-controlled loop (the body of the loop is typically executed a specific number of times).

For Loop Diagram

The initialize clause executes only once and is typically used to set the initial value of the counter. The condition clause tests the counter to see if it has reached its final value. This test occurs before the statements in the body of the loop are executed (making it a pre-test loop). The update clause typically increments the counter and is executed immediately after the statements in the body of the loop. For example, the loop below will execute exactly 4 times.

for(count = 0; count < 4; count = count + 1) {
    body_of_loop
}

The variable, count, indicates the number of times the statements in the body of the loop have been executed. It starts at zero to indicate that the statements have not yet been executed. The execution of this looping structure proceeds as indicated in the table below (reading right to left and top to bottom).

IterationInitializationConditionBody_of_LoopUpdate
 Set count to 0   
1 count (0) < 4 is trueExecuteAdd 1 to count
2  count (1) < 4 is trueExecuteAdd 1 to count
3 count (2) < 4 is trueExecuteAdd 1 to count
4 count (3) < 4 is trueExecuteAdd 1 to count
  count (4) < 4 is falseSkip 

The loop terminates when the condition, count < 4, becomes false.

The for loop is a convenience. We could use a while loop to do exactly the same thing as the for loop above:

count = 0;
while(count < 4) {
    body_of_loop
    count = count + 1; // The update is part of the body of the loop
}

Example ◄Click to see an example of a for loop.