You are on page 1of 4

CONTROL FLOW STATEMENTS

1. ForNext Statement
Repeats a group of statements a specified number of times.
Syntax:
For counter = start To end [Step step]
[statements]
[Exit For]
[Statements]
Next

Arguments
counter
Numeric variable used as a loop counter. The variable can't be an array
element or an element of a user-defined type.
start
Initial value of counter.
end
Final value of counter.
step
Amount counter is changed each time through the loop. If not specified,
step defaults to one.
statements
One or more statements between For and Next that are executed the
specified number of times.
We can nest For...Next loops by placing one For...Next loop within another. Give
each loop a unique variable name as its counter. The following construction is
correct:
For I = 1 To 10
For J = 1 To 10
For K = 1 To 10
. . .
Next
Next
Next

************************************************************************
2. WhileWend Statement
Executes a series of statements as long as a given condition is True.
Syntax:
While condition
Version [statements]
Wend

Arguments
condition
Numeric or string expression that evaluates to True or False. If condition is
Null, condition is treated as False.
statements
One or more statements executed while condition is True.
If condition is True, all statements in statements are executed until the Wend
statement is encountered.
Control then returns to the While statement and condition is again checked.
If condition is still True, the process is repeated.
If it is not True, execution resumes with the statement following the Wend
statement.
************************************************************************
3. IfThenElse Statement
Conditionally executes a group of statements, depending on the value of an
expression.
If condition Then statements [Else elsestatements ]

Or, you can use the block form syntax:


If condition Then
[statements]

[ElseIf condition-n Then


[elseifstatements]] . . .
[Else
[elsestatements]]
End If

Arguments
condition
One or more of the following two types of expressions:
A numeric or string expression that evaluates to True or False. If condition is
Null, condition is treated as False.
An expression of the form TypeOf objectname Is objecttype. The objectname
is any object reference and objecttype is any valid object type. The expression
is True if objectname is of the object type specified by objecttype; otherwise it
is False.
statements
One or more statements separated by colons; executed if condition is True.
condition-n
Same as condition.
elseifstatements
One or more statements executed if the associated condition-n is True.
elsestatements
One or more statements executed if no previous condition or condition-n
expression is True
************************************************************************
4. SelectCase Statement

Executes one of several groups of statements, depending on the value of an


expression.

A Select Case statement provides capability similar to the If...Then...Else


statement, but it makes code more efficient and readable.
Select Case testexpression
[Case expressionlist-n
[statements-n]] . . .
[Case Else expressionlist-n
[elsestatements-n]]
End Select

Arguments
testexpression
Any numeric or string expression.
expressionlist-n
Required if Case appears. Delimited list of one or more expressions.
statements-n
One or more statements executed if testexpression matches any part of
expressionlist-n.
elsestatements-n
One or more statements executed if testexpression doesn't match any of
the Case clauses.

You might also like