A decision or conditional statement represents a branch in the program. It marks a place where the program can execute one set of statements or another, or possibly no statements at all, depending on some condition. These include several kinds of If statements, Choose statements, and Select Case statements.
The single-line If Then statement has two basic forms. The first allows the program to execute a single statement if some condition is True. The syntax is as follows:
If condition Then statement
If the condition is True, the program executes the statement. In the most common form of single-line If Then statements, the statement is a single simple command (such as assigning a value to a variable or calling a subroutine).
The following example checks the emp object’s IsManager property. If IsManager is True, the statement sets the emp object’s Salary property to 90,000.
If emp.IsManager Then emp.Salary = 90000
The second form of the single-line If Then statement uses the Else keyword. The syntax is as follows:
If condition Then statement1 Else statement2
If the condition is True, the code executes the first statement. If the condition is False, the code executes the second statement. The decision about which statement to execute is an either-or decision; the code executes one statement or the other, but not both.
This type of single-line If Then Else statement can be confusing if it is too long to easily see in the code editor. For longer ...