- 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.5 If...Then Selection Statement
A selection statement chooses among alternative courses of action. Suppose that the passing grade on an examination is 60 (out of 100). Then the pseudocode statement
If student's grade is greater than or equal to 60 then Display "Passed"
determines whether the condition "student's grade is greater than or equal to 60" is true or false. If the condition is true, then "Passed" is displayed, and the next pseudocode statement in order is "performed" (remember that pseudocode is not a real programming language). If the condition is false, the display statement is ignored, and the next pseudocode statement in order is "performed."
The preceding pseudocode If statement may be written in Visual Basic as
|
The code corresponds closely to the pseudocode, showing the usefulness of pseudocode as a program-development tool. The statement in the body of the If...Then statement displays the string "Passed" on resultLabel.
Whitespace
The compiler ignores whitespace, such as spaces, tabs and blank lines used for indentation and vertical spacing, unless the whitespace is contained in strings. Some whitespace is required, however, such as the space between variable names and keywords. You can insert extra whitespace characters to enhance program readability.
Single Line If...Then statement
The preceding If...Then selection statement also could be written on a single line as
If studentGrade >= 60 Then resultLabel.Text = "Passed"
In the multiple-line format, all statements (there can be many) in the If...Then's body execute if the condition is true. In the single-line format, only the statement immediately after the Then keyword executes if the condition is true.
UML Activity Diagram for the If ... Then Selection Statement
Figure 4.2 is an activity diagram for the single-selection If...Then statement. It contains what is perhaps the most important symbol in an activity diagram—the diamond, or decision symbol, which indicates that a decision is to be made. A decision symbol indicates that the workflow will continue along a path determined by the symbol's associated guard conditions, which can be true or false. Each transition arrow emerging from a decision symbol has a guard condition (specified in square brackets above or next to the transition arrow). If a particular guard condition is true, the workflow enters the action state to which that transition arrow points. Exactly one of the guard conditions associated with a decision symbol must be true when a decision is made. In Fig. 4.2, if the grade is greater than or equal to 60, the program displays "Passed" on the screen, then transitions to the final state of this activity. If the grade is less than 60, the program immediately transitions to the final state without displaying a message.
Fig. 4.2 If ... Then single-selection statement activity diagram.