Visual Basic procedures utilize Flow Control Constructs for decision making, looping, and more efficient processing. Without flow control the code executes left to right and top to bottom.
Oddly enough, if the condition is NULL, then the condition is treated as False.
If Condition Then Statement
If Condition Then Statements End If
If Condition-1 Then StatementBlock-1 [ElseIf Condition2 Then StatementBlock-2] [Else StatementBlock-n] End If
Use in place of If...Then when there is a single test expression for multiple possibilities.
Select Case TestExpression Case ExpressionList-1 StatementBlock-1 [Case ExpressionList-2 StatementBlock-2] ... [Case Else StatementBlock-n] End Select
Some example cases and expression lists:
Case 7
Case 1 To 4, 7 To 9, 11, 13, Is > MaxNumber
Case "everything", "nuts" To "soup", TestItem
Use when you must repeat a statement a known number of times.
For Counter = Start To End [Step Increment]
Statements
Next [Counter]
'The Step defaults to 1 if not specified.
For Each Element In Group
Statements
Next [Element]
'Use when working with a collection of objects or an array.
Use when you must repeat a statement an unknown number of times.
Do While Condition
Statements
Loop
'Use when looping zero or more times.
Do
Statements
Loop While Condition
'Use when looping at least once.
Do Until Condition
Statements
Loop
'Use when looping zero or more times.
Do
Statements
Loop Until Condition
'Use when looping at least once.
While Condition
Statements
Wend
'Just like the Do While...Loop but it is archaic syntax.
Use to specify a number of properties for a single object. Can also be used to execute a number of methods of the object.
With ObjectName
['Statements using .Property or .Method]
End With
A function that evaluates a list of paired expressions and values, then returns the value whose expression is true.
Switch (Expression-1, Value-1[..., Expression-n, Value-n])
A function that takes an index to return one of a list of choices.
Choose (Index, Choice-a[..., Choice-n])
Exit Sub, Exit Function, Exit Property, Exit Do, or Exit For keywords.Page Modified: (Hand noted: 2007-09-20 19:50:52Z) (Auto noted: 2007-11-17 06:37:25Z)