Control Structures


If...Then...Else

If condition1 Then 
     statements1
Else
     statements2
End If

If condition1 is True, then statements1 is executed; Else, condition1 is not True, therefore statements2 gets executed. The structure must be terminated with the End If statement.

The Else clause is optional. In a simple comparison, statements1 get executed or not.
If condition1 Then
     statements1
End If

Select Case

Can be used as an alternative to the If...Then...Else structure, especially when many comparisons are involved.
Select Case ShirtSize
     Case 1
          SizeName.Caption = "Small"
     Case 2
          SizeName.Caption = "Medium"
     Case 3
          SizeName.Caption = "Large"
     Case 4
          SizeName.Caption = "Extra Large"
     Case Else
          SizeName.Caption = "Unknown size"
End Select

Do...Loop

Used to execute a block of statements an unspecified number of times.
Do While condition

     statements

Loop

First, the condition is tested; if condition is True, then the statements are executed. When it gets to the Loop it goes back to the Do and tests condition again. If condition is False on the first pass, the statements are never executed.



For...Next

When the number of iterations of the loop is known, it is better to use the For...Next rather than the Do...Loop.
For counter = start To end

     statements

Next

1) The counter is set to the value of start.
2) Counter is checked to see if it is greater than end; if yes, control passes to the statement after the Next; if not the statements are executed.
3)At Next, counter is incremented and goes back to step 2

Comments

Popular posts from this blog

NICOSIA