Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 5: Decision
Last change: February 13 2016 18:47:58.
<<PreviousNext>>

Nesting If Blocks

Any of the If blocks can be further nested to handle complex conditions. As an illustration, assume a teacher’s grading system requires that a student must have not only a score of 90 or better, but also a perfect attendance to earn an A; otherwise, a B is given. A student with a score in the range of 80 to 89 must have at least a 90% attendance record to earn a B. All other students will get a C. These decision rules are depicted in the flowchart in Figure 5-2.

Figure 5-2
Flowchart of grade assignment
 

The two text boxes in the user interface shown in Figure 5-3 are used to enter both the score (txtScore) and the attendance percentage (txtAttendance). When the user clicks on the Show Grade button (btnGrade), the grade is displayed in the label named lblGrade. The following code provides a solution:

Private Sub btnGrade_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnGrade.Click Dim Score As Integer Dim Attendance As Integer Dim Grade As String Score = Val(txtScore.Text) Attendance = Val(txtAttendance.Text) If Score >= 90 Then If Attendance = 100 Then Grade = “A” Else Grade = “B” End If
ElseIf Score >= 80 AndAlso Attendance >= 90 Then Grade = “B” Else Grade = “C” End If lblGrade.Text = Grade End Sub

In the preceding code, the score and attendance percentage are obtained from the two text boxes, txtScore and txtAttendance, respectively. The score is first compared with 90. If it is greater or equal to 90, the second-level If statement checks whether the attendance percentage is equal to 100. If so, an A grade is assigned; otherwise, a B grade is assigned. The ElseIf statement checks whether the score is greater than or equal to 80 and the attendance is at least 90. If so, a B grade is assigned; otherwise, a C grade is assigned. The resulting grade is displayed in a text box, named txtGrade.

Figure 5-3
Grade assignment

Will This Work?

You may wonder why not just code as follows to solve the previous problem:

If Score>=90 AndAlso Attendance = 100 Then Grade = “A” ElseIf Score >= 80 AndAlso Attendance >= 90 Then Grade = “B” Else Grade = “C” End If txtGrade.Text = Grade  
This would appear much simpler. But the modified code would not work correctly. The initial problem states that if a student has a score of at least 90 as well as perfect attendance, they will earn an A. Without perfect attendance, they will earn a B. The original solution produces the correct result. The modified code will assign a C to a student who has a score of 100 but with an 80% attendance! The 80% attendance will fail not only the If test, but also the ElseIf test, thus falling into the catch all Else block for an assignment of a grade C.

Rule for Nesting

When If blocks are nested, one rule to keep in mind is that one block should enclose the other, constituting a relationship of the inner and outer blocks. It is not logical (and, therefore, not allowed) to intertwine any If blocks. Figure 5-4 shows both the acceptable and unacceptable nesting blocks.

If Blocks: A Summary of Syntax

In terms of syntax, the If block can be structured several ways. The following table provides a summary.

Syntax Structure Example Remark
If condition Then statement If rbtMale.Checked Then Sex = “M” This is not a preferred coding structure.
If Condition Then Statement(s) End If If Len(txtName.Text) = 0 Then MsgBox(“Must have a name”) e.Cancel = True End If Use this structure to handle situations in which one or more statements must be executed when a certain condition is True, but no action is required when the condition is False.
If Condition Then Statements Else Statements End If If chkPrintAll.Checked Then lstZipCode.Enabled = False Else lstZipCode.Enabled = True End If Use this structure to handle situations in which a group of statements need to be executed when the condition is True; otherwise, another group of statements are to be executed.
If Condition1 Then Statement group 1 ElseIf Condition2 Then Statement group 2 ElseIf Condition3 Then Statement group 3 Else Statement group 4 End If If rbtBreakfast.Checked Then Charge = 4.50 ElseIf rbtLunch.Checked Then Charge = 7.50 ElseIf rbtDinner.Checked Then Charge = 10.50 Else Charge = 0 End If Use this structure when a series of mutually exclusive conditions will require different actions, or cause different results.

Figure 5-4
Legitimate and illegitimate nesting of If blocks

Last change: February 13 2016 18:47:57.
<<PreviousNext>>