Search This Blog

Dual Alternative Decision Structures

CONCEPT: A dual alternative decision structure will execute one group of statements if its Boolean expression is true, or another group if its Boolean expression is false.

A dual alternative decision structure has two possible paths of execution—one path is taken if a condition is true, and the other path is taken if the condition is false. Figure 4-8 shows a flowchart for a dual alternative decision structure.





 The decision structure in the flowchart tests the condition temperature < 40. If this condition is true, the statement Display "A little cold, isn't it?" is performed. If the condition is false, the statement Display “Nice weather we’re having.” is performed.

In pseudocode we write a dual alternative decision structure as an If-Then-Else statement. Here is the general format of the If-Then-Else statement:



In the general format, the condition is any Boolean expression. If the expression is true, the statements that appear next are executed, up to the line that reads Else. If the expression is false, the statements that appear between Else and End If are executed. The line that reads End If marks the end of the If-Then-Else statement.

The following pseudocode shows an example of an If-Then-Else statement. This pseudocode matches the flowchart that was shown in Figure 4-8.

If temperature < 40 Then
   Display "A little cold, isn't it?"
Else
   Display "Nice weather we're having."
End If


We will refer to the line that reads Else as the Else clause. When you write an If-Then-Else statement, use the following style conventions:

    Make sure the If clause, the Else clause, and the End If clause are aligned.

    Indent the conditionally executed statements that appear between the If clause and the Else clause, and between the Else clause and the End If clause.
 

Leave a Reply