- 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.7 Nested If...Then...Else Statements
Nested If...Then...Else statements test for multiple conditions by placing If...Then...Else statements inside other If...Then...Else statements. For example, the pseudocode in Fig. 4.4 displays "A" for exam grades greater than or equal to 90, "B" for grades in the range 80–89, "C" for grades in the range 70–79, "D" for grades in the range 60–69 and "F" for all other grades. The pseudocode may be written in Visual Basic as shown in Fig. 4.5.
Fig 4.4. Pseudocode for nested If...Then...Else statements that display a letter grade based on a numeric grade value.
1 If student's grade is greater than or equal to 90 then 2 Display "A" 3 Else 4 If student's grade is greater than or equal to 80 then 5 Display "B" 6 Else 7 If student's grade is greater than or equal to 70 then 8 Display "C" 9 Else 10 If student's grade is greater than or equal to 60 then 11 Display "D" 12 Else 13 Display "F" |
Fig 4.5. nested If...Then...Else statements that correspond to the pseudocode in Fig. 4.4.
1 If studentGrade >= 90 Then 2 resultLabel.Text = "A" ' display "A" 3 Else 4 If studentGrade >= 80 Then 5 resultLabel.Text = "B" ' display "B" 6 Else 7 If studentGrade >= 70 Then 8 resultLabel.Text = "C" ' display "C" 9 Else 10 If studentGrade >= 60 Then 11 resultLabel.Text = "D" ' display "D" 12 Else 13 resultLabel.Text = "F" ' display "F" 14 End If 15 End If 16 End If 17 End If |
If studentGrade is greater than or equal to 90, the first four conditions are true, but only the statement at line 2 executes (causing "A" to be displayed). After that statement executes, the Else part of the "outer" If...Then...Else statement (line 3–17) is skipped, and the program proceeds with the next statement after the last EndIf.
ElseIf
Most programmers prefer to write the nested If...Then...Else statements from Fig. 4.5 using the ElseIf keyword as shown in Fig. 4.6. Both forms are equivalent, but the latter is popular because it avoids deeply indenting the code and makes it more readable. In nested If...Then...Else statements, if you type Else If on one line, the Visual Basic editor will automatically convert it to ElseIf and indent the code as in Fig. 4.6.
Fig 4.6. Nested If...Then...Else statements from Fig. 4.5 reimplemented using ElseIf.
1 If grade >= 90 Then 2 resultLabel.Text = "A" ' display "A" 3 ElseIf grade >= 80 Then 4 resultLabel.Text = "B" ' display "B" 5 ElseIf grade >= 70 Then 6 resultLabel.Text = "C" ' display "C" 7 ElseIf grade >= 60 Then 8 resultLabel.Text = "D" ' display "D" 9 Else 10 resultLabel.Text = "F" ' display "F" 11 End If |