What makes programming languages flexible and capable of handling every situation and programming challenge with a relatively small set of commands is the capability to examine external conditions and act accordingly. Programs aren’t monolithic sets of commands that carry out the same calculations every time they are executed. Instead, they adjust their behavior depending on the data supplied; on external conditions, such as a mouse click or the existence of a peripheral; or even abnormal conditions generated by the program itself. For example, a program that calculates averages may work time and again until the user forgets to supply any data. In this case, the program attempts to divide by zero and your program must detect this condition and act accordingly.
An application needs a built-in capability to test conditions and take a different course of action depending on the outcome of the test. Visual Basic provides three control flow, or decision, structures:
- If Then
- If Then … Else
- Select Case
The If …Then structure tests the condition specified, and if it’s True, executes the statements that follow. The If structure can have a single-line or a multiple-line syntax. To execute one’ statement conditionally, use the single-line syntax as follows:
If condition Then statement
Visual Basic evaluates the condition, and if it’s True, executes the statement that follows. If the condition is False, it continues with the statement following the If structure.
You can also execute multiple statements by separating them with a colon:
If condition Then statement: statement: statement
Here’s an example of a single-line If statement:
If Month(date) = 1 Then Year – Year + 1
You can break this statement into multiple lines, as shown here:
If Month(date) = 1 Then
Year = Year + 1
Some programmers prefer the multiple-line syntax of the If… Then statement, even if it contains a single statement, because the code is easier to read.
If …Then …Else
A variation of the If… Then statement is the If… Then .. Else statement, which executes one block of statements if the condition is True and, another if the condition is False. The syntax of the If… Then … Else statement is as follows:
‘Another variation of the If… Th~n … Else statement uses several conditions, with the Else-If keyword:
You can have any number of ElseIf clauses. The conditions are evaluated from the top, and if one of them is True, the corresponding block of statements is executed. The Else clause will be executed if none of the previous expressions are True. Here’s an example of an If statement with ElseIf clauses:
You may have noticed that the order of the comparison’) is vital in a nested If … Then structure that uses Elself statements. Had you written the previous code segment with the first two conditions switched, like this:
the results would be quite unexpected. The code would (compare the score variable (49) to the value 75. Since 49 is less than 75, it would assign the value “Pass” to the variable Result and then it would skip the remaining clauses. Thus, a student who made 49 would have passed the test! So, be extremely careful and test your code thoroughly if it uses multiple Elself clauses.
An alternative to the efficient, but difficult-to-read, code of the multiple ElseIf . structure is the Select Case statement.
The Select Case structure compares one expression to different values. The advantage of the Select Case statement over multiple If… Then … Else statements is that it makes the code easier to read and maintain.
The Select Case structure tests a single expression, which is evaluated once at the top of the structure. The result of the test is then compared with several values, and if it matches one of them, the corresponding block of statements is executed. Here’s the syntax of the Select Case statement:
The expression variable, which is evaluated at the beginning of the statement, is the number’ of the weekday, as reported by the WeekDay() function (a value in the range 1 to 7). The value of the expression is then compared with the values that follow each Case keyword. If they match, the block of statements up to the next Case keyword is executed, and the program skips to the statement following the End Select statement. The block of the Case Else statement is optional and is executed if none of the previous Case values match the expression,
Some case statements can be followed by multiple values, which are separated by Commas. Here’s a revised version of the previous example:
The five workdays and the two.weekend days are handled by two Case statements with multiple values. This structure doesn’t contain a Case Else statement because an values are examined in the Case statements. The WeekDay() function can’t return another value.
For comparison, here are the equivalent If… Then … Else statements that would implement the previous example:
To say the least, this coding is verbose. If you attempt to implement a more elaborate Select Case statement with If… Then … Else statements, the code becomes even more difficult to read. Here is the first example, implemented with If … Then … Else statements:
Of course, the Select Case statement can’t be-substituted for any If…Then structures. The Select Case structure only evaluates the expression at the beginning. B. contrast, the If … Then … Else structure can evaluate a different expression for each ElseIf statement.