The If…Then statement examines the truthfulness of an expression. Structurally, its formula is:
If ConditionToCheck Then Statement
Therefore, the program examines a condition, in this case ConditionToCheck. This ConditionToCheck can be a simple expression or a combination of expressions. If the ConditionToCheck is true, then the program will execute the Statement.
There are two ways you can use the If…Then statement. If the conditional formula is short enough, you can write it on one line, like this:
If ConditionToCheck Then Statement
If there are many statements to execute as a truthful result of the condition, you should write the statements on alternate lines. Of course, you can use this technique even if the condition you are examining is short. In this case, one very important rule to keep is to terminate the conditional statement with End If. Here is an example:
If ConditionToCheck Then
Statement
End If
Here is another example:
If Condition Then
Statement1
Statement2
StatementnEnd If
The If…Then statement offers only one alternative: to act if the condition is true. Whenever you would like to apply an alternate expression in case the condition is false, you can use the If…Then…Else statement. The formula of this statement is:
If ConditionToCheck Then
Statement1
Else
Statement2
End If
When this section of code is executed, if the ConditionToCheck is true, then the first statement, Statement1, is executed. If the ConditionToCheck is false, the second statement, in this case Statement2, is executed.
The If…Then…ElseIf statement acts like the If…Then…Else expression, except that it offers as many choices as necessary. The formula is:
If Condition1 Then
Statement1
ElseIf Condition2 Then
Statement2
ElseIf Conditionk Then
Statementk
End If
The program will first examine Condition1. If Condition1 is true, the program will execute Statment1 and stop examining conditions. If Condition1 is false, the program will examine Condition2 and act accordingly. Whenever a condition is false, the program will continue examining the conditions until it finds one. Once a true condition has been found and its statement executed, the program will terminate the conditional examination at End If.
There is still a possibility that none of the stated conditions is true. In this case, you should provide a “catch all” condition. This is done with a last Else section. The Else section must be the last in the list of conditions and would act if none of the primary conditions is true. The formula to use would be:
If Condition1 Then
Statement1
ElseIf Condition2 Then
Statement2
ElseIf Conditionk Then
Statementk
Else
CatchAllStatement
End If
If you have a large number of conditions to examine, the If…Then…Else will go through each one of them. Visual Basic offers the alternative of jumping to the statement that applies to the state of the condition.
The formula of the Select Case is:
Select Case Expression
Case Expression1
Statement1
Case Expression2
Statement2
Case Expressionk
Statementk
End Select
The Expression will examined and evaluated once. Then it will compare the result of this examination with the Expression of each case. Once it finds one that matches, it would execute the corresponding Statement.
If you anticipate that there could be no match between the Expression and one of the Expressions, you can use a Case Else statement at the end of the list. The statement would then look like this:
Select Case Expression
Case Expression1
Statement1
Case Expression2
Statement2
Case Expressionk
Statementk
Case Else
Statementk
End Select