Visual Basic provides three decision
control flow structures:
• If...Then
• If...Then...Else
• Select Case
If...Then: The
If...Then structure tests the condition specified, and if it’s True,
executes the statement(s) 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
You can also execute multiple
statements by separating them with a colon:
If
condition Then statement: statement: statement
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:
If condition1 Then
statementblock-1
ElseIf condition2 Then
statementblock-2
ElseIf condition3 Then
statementblock-3
Else
statementblock-4
End If
Select Case: 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:
Select Case expression
Case value1
statementblock-1
Case value2
statementblock-2
.
.
.
Case Else
statementblock
End Select
Loop Statements: Loop statements allow you to execute one or more
lines of code repetitively. Many tasks consist of trivial operations that must
be repeated over and over again, and looping structures are an important part
of any programming language. Visual Basic supports the following loop
statements:
• Do...Loop
• For...Next
• While...Wend
Do...Loop: The Do...Loop executes a block of statements for as long as a condition
is True. Visual Basic evaluates an expression, and if it’s True, the statements
are executed. If the expression is False, the program continues and the
statement following the loop is executed.
There are two
variations of the Do...Loop statement and both use the same basic model. A loop
can be executed either while the condition is True or until the condition
becomes True. These two variations use the keywords While and Until
to specify how long the statements are executed. To execute a block of
statements while a condition is True, use the following syntax:
Do While condition
statement-block
Loop
To execute a
block of statements until the condition becomes True, use the following syntax:
Do Until condition
statement-block
Loop
Another
variation of the Do loop executes the statements first and evaluates the condition
after each execution. This Do loop has the following syntax:
Do
statements
Loop While condition
or
Do
statements
Loop Until condition
For...Next: The For...Next loop is one of the oldest loop structures in
programming languages. Unlike the Do loop, the For...Next loop requires that
you know how many times the statements in the loop will be executed. The
For...Next loop uses a variable (it’s called the loop’s counter) that
increases or decreases in value during each repetition of the loop. The
For...Next loop has the following syntax:
For counter = start To end [Step increment]
statements
Next [counter]
The keywords in the square brackets
are optional.
While...Wend: The While...Wend loop executes a block of statements while a condition is
True. The While...Wend loop has the following syntax:
While condition
statement-block
Wend