Multiple Decisions Using If

Multiple Decisions Using If

Life is rarely limited to the either/or type of decisions that the If-Then-Else statement is designed to resolve. Life seems to be filled with lots of gray areas ”lots of maybe and perhaps. For example, suppose you are writing a program to price movie tickets. Those under 12 get one price, those over 65 get another price, and those in between these two ages get a third price.

One way to attack this problem is to nest an If statement. To nest an If statement, you place one If statement inside another If statement. Consider the following code fragment:

 Dim TicketPrice as Double  ' Assume Age is set somewhere else If Age <= 12 Then  TicketPrice = 2.5    ' Child price Else  If Age >= 65 Then    ' Start of the nested If   TicketPrice = 6.0  ' Senior price  Else   TicketPrice = 8.0  ' Regular price  End If        ' End nested If End If 

You need to assume that Age is determined somewhere else in the program. All you are concerned with in this example is the pricing of the ticket based on Age . The first If expression checks whether Age is less than or equal to 12 . If it is, the Then statement block is executed, and TicketPrice is set to 2.5 .

If Age is greater than 12 , the Else statement block is executed. However, in this example, the Else statement block contains another If statement. That is, a second If block is contained within the first If block. This second If statement block is the nested If statement. The nested If checks whether Age is equal to or greater than 65 . If it is, the individual must be a senior citizen, and the (nested) Then statement block is executed. The result is that TicketPrice is set to 6.0 .

If the person is not 65 or older and not 12 or younger , he or she must be between 13 and 65 years of age, and TicketPrice is set to 8.0 . You know the person's age must fall within these limits because of the first If test that was performed on Age earlier.

The ElseIf Statement

Nested If blocks like the ticket price example in the previous section are so common that Visual Basic .NET provides a semi-condensed expression syntax using the ElseIf keyword. You can rewrite the If statements from the previous section by using the ElseIf keyword as follows :

 If Age <= 12 Then   TicketPrice = 2.5    ' Child price ElseIf Age >= 65 Then    ' Start of the nested If   TicketPrice = 6.0  ' Senior price Else   TicketPrice = 8.0  ' Regular price End If 

If you compare this code fragment with the one in the previous section, you should find that the ElseIf version does away with one End If statement and squishes the Else and If keywords into a single ElseIf keyword. Except for some minor indentation changes, everything else is the same for the two versions.

So, which is better: the nested If or the ElseIf ? Because the decision has no impact on the underlying code, the choice is more a matter of style than of substance. You should use the one you prefer.

One thing you should avoid is a cascading If block. A cascading If block occurs when you have one If block nested within another If block, nested within another If block, nested within another If block, and so on. Cascading If blocks are more difficult to read than they need be.

The need for such nested blocks, however, is more frequent than you might expect. For example, suppose you have a variable named MyDay that assumes the values 1 through 7 to represent each day of the week. Further assume that you want to do something different on each day of the week. You could write this as a series of nested If blocks, but there is a better way: You can use the Select Case statement, as described in the following section.

Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: