O'Reilly logo

Visual Basic 2012 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DECISION STATEMENTS

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.

Single-Line If Then

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required