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
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.

Additional Notes on Coding the If Block

Because most programming problems involve many decisions, If blocks are used extensively in programs. It is desirable to consider several issues before leaving this topic.


When If blocks are used, the programming logic becomes less than straightforward because some statements may or may not be executed based on the result of the If expression. Keeping track of all possible results can become challenging. Therefore, it is important to provide as much visual aid as possible in the code to facilitate future review and current logic development/tracing. 
One important aspect of this is to indent the code properly. Fortunately, the IDE editor indents your code automatically. Basically, all statements in each portion of the If block are indented. In a nested block, the inside If block skeleton (If, ElseIf, Else, and End If) is indented just like the inside statements. In addition, the statements inside the inner If block are further indented. Such a layout clearly indicates the level of logical nesting. (Note that the compiler will even catch your failure to include a matching End If statement.) After completing the coding of a complex structure of nested If block, take advantage of this extra help by the editor and exam the structure carefully for hints about proper placement of your code block.

Use of Comments

The importance of using comments (remarks) in programs cannot be overemphasized. Comments can be especially helpful to people who review code that contains many If statements. In many cases, the meaning of the expressions (conditions) in the If statement is not self-explanatory. Comments help you and the reviewer follow the logic of the code much more easily. Consider the following statement:

If KeyAscii <Keys.Space Then

Without any comments with it, it may take a while for a beginner to figure out what the If statement is attempting to accomplish. Adding a remark similar to the following line can make it much easier for anybody who reads the code:

‘ These are control keys and can be ignored

Syntax and Semantic

Beware that you can be syntactically correct, but end up with a wrong result. Consider the following code:

If 21 <= Age <=35 Then MsgBox(“You are a young adult”) End If 
The code is meant to test whether the variable, Age is in the range of 21 to 35; however, if you test the code, you will find that the message box always displays “You are a young adult” even when Age is 100! Why? In the execution, 21 is first compared with Age to see whether the relation <= is True, yielding an implicit numeric value of either –1 (for True) or zero (for False). When this result is compared with 35, it is always less than 35, so the result is always True.
Note that you can obtain the above result only if you turn off Option Strict (the default). If you set Option Strict On, the compiler will tell you that the implicit conversion from Boolean (result of 21 <= Age) to the data type of 35 (say, Integer) is not allowed. Another good reason to set Option Strict On, isn’t it?


The correct code to check the age range of 21 and 35 is:
If  21 <=Age AndAlso Age <= 35 Then


Another issue concerning the use of the If block relates to the efficiency (speed of execution) versus the clarity of code. Consider this line of code (used in some countries to determine whether a person is a candidate for military draft):

If Sex = “M” And Age >=16 Then

Although the line of code is clear, it is not as efficient as the following code:

If Sex = “M” AndAlso Age >= 16 Then

In the first code sample, three operations are always involved in the expression: Sex is compared with “M,” Age is compared with 16, and an And operation is performed on the results from the two comparisons. On the other hand, in the second code sample, when the first expression is false (Sex is not equal to “M”), the AndAlso operator will not evaluate the second expression but conclude that the result is false; therefore, this operator is more efficient than the And operator. Note that the use of the AndAlso operator has the same effect as the following code structure:

If Sex = “M” Then If Age >= 16 Then

As you can see, the code line using the AndAlso operator appears to be simpler and should be the preferred way of coding.

Note also the order of the expressions in the If condition can affect the efficiency. The probability of a person being a male is approximately 50%, but the probability of a person being 16 or older is much higher. With the current code, the chance that the second expression is tested is only one half. If the order of the expressions is reversed, however, the chance that Age >= 16 is true is much higher, resulting in more need to evaluate the second expression (Sex = “M”) before a conclusion can be reached. The current code is more efficient than if you reverse the order of the two expressions.
Similarly, the OrElse operator is more efficient than the Or operator in evaluating the condition in the If statement. Because the OrElse operator stops evaluating the second expression as soon as it finds the first expression in the condition is true, you should place those expressions with higher probability of being true first. For example, suppose you are writing a program to diagnose a disease that always causes abnormal body temperature mostly fever but occasionally below normal. Your code will be more efficient if you write:

If Temperature >100 OrElse Temperature < 97

Code Clarity and Alternatives

As already mentioned, when the code involves too many If blocks (especially nested ones), it can become challenging to trace the logic. The effects of the code can become much harder to figure out. Are there any alternatives? Yes, the possibilities include the following:

Last change: February 13 2016 18:47:58.