VB's If-Then-Else Control Structure



The one-statement If-Then-Else:

If <condition> Then <tstmnt> Else <fstmnt>
<condition>
is a boolean expression. <tstmnt> is a statment executed if <condition> is true. <fstmnt> is a statment executed if <condition> is false. When an "If without Else" is appropriate the Else <fstmnt> may be omitted.

The block If-Then-Else:

If <condition> Then
	<statements done if true>
Else
	<statements done if false>
End If
<statements done if true>
and <statements done if false> may be 0 or more statements. Good programming practice is to indent these statements 2 to 4 spaces or a tab stop. The
Else
	<statements done if false>
may be omitted if appropriate.

The If-Then-ElseIf:

If <condition0> Then
	<statements done if condition0 true>
ElseIf <condition1> Then
	<statements done if condition1 true>
ElseIf <condition2> Then
	<statements done if condition2 true>
Else
	<statements done if all conditions false>
End If
This control structure is one way to implement multiway selection in VB. Although only 2 ElseIf blocks are illustrated above, there may be as many of these and their associated conditions as needed.

<statements done if ...> may all be 0 or more statements and should be indented. The Else block may be omitted as in the block If-Then-Else.