Control Flow Constructs
You may be familiar with VB language elements, such as If . . . Then . . . Else, that are
used to control the flow of your program. This section is an overview of those constructs.
If . . . Then . . . Else
The conditional construct If . . . Then . . . Else first checks an expression that must
resolve to a Boolean value. Then based on that value, the statements that you specify are executed.
The general syntax of this construct is
If condition Then
statements
ElseIf elseifcondition Then
elseifstatements
Else
elsestatements
End If
Here??™s an If statement in its simplest formwith an If condition, statements, and an End If:
CHAPTER 2 n VB 2008 SYNTAX 29
If x = 0 Then
y = 1
End If
You can also write this on a single line, although from a stylistic point of view, it is less readable,
and as soon as you need to add more statements, you??™ll need a corresponding End If:
If x = 0 Then y = 1
If the first If condition evaluates to False, the program flow will go to the first ElseIf condition
and evaluate it. If conditions evaluate to False, execution continues to each subsequent
ElseIf and then the Else statement:
If x = 0 Then
y = 1
ElseIf x = 1 Then
y = 10
End If
Select . . . Case
VB also offers another conditional construct called Select .
Pages:
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82