- 4.1 Introduction
- 4.2 Algorithms
- 4.3 Pseudocode
- 4.4 Control Structures
- 4.5 If...Then Selection Statement
- 4.6 If...Then...Else Selection Statement
- 4.7 Nested If...Then...Else Statements
- 4.8 Repetition Statements
- 4.9 Compound Assignment Operators
- 4.10 Formulating Algorithms: Counter-Controlled Repetition
- 4.11 Formulating Algorithms: Nested Control Statements
- 4.12 Using the Debugger: Locating a Logic Error
- 4.13 Wrap-Up
- Summary
- Terminology
- Self-Review Exercises
- Answers to Self-Review Exercises
- Quick Quiz
- Exercises
- Making a Difference Exercises
4.6 If...Then...Else Selection Statement
The If...Then...Else selection statement allows you to specify that a different action (or sequence of actions) is to be performed when the condition is true than when the condition is false. For example, the pseudocode statement
If student's grade is greater than or equal to 60 then Display "Passed" Else Display "Failed"
displays "Passed" if the student's grade is greater than or equal to 60, and displays "Failed" if the student's grade is less than 60. In either case, after the display occurs, the next pseudocode statement in sequence is "performed."
The preceding pseudocode If...Else statement may be written in Visual Basic as
|
The body of the Else clause is indented so that it lines up with the body of the If clause.
UML Activity Diagram for the If...Then...Else Selection Statement
Figure 4.3 illustrates the flow of control in the If...Then...Else statement.
Fig. 4.3 If ... Then ... Else double-selection statement activity diagram.