Second Lecture Note VB
Second Lecture Note VB
Example
For I = 1 to 50 Step 2
A=I*2
Debug.Print A
Next I
• You may exit a For/Next loop using an Exit For
statement. This will transfer program control to
the statement following the Next statement.
Count.
If Age = 5 Then
Category = "Five Year Old"
ElseIf Age >= 13 and Age <= 19 Then
Category = "Teenager"
ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >= 60 and Age <=
65) Then
Category = "Special Adult"
ElseIf Age > 65 Then
Category = "Senior Citizen"
Else
Category = "Everyone Else“
End If
Example of case statement
Select Case Age
Case 5
Category = "Five Year Old"
Case 13 To 19
Category = "Teenager"
Case 20 To 35, 50, 60 To 65
Category = "Special Adult"
Case Is > 65
Category = "Senior Citizen"
Case Else
Category = "Everyone Else"
End Select
Do While/Loop
Do While/Loop Example:
Counter = 1
Do While Counter <= 1000
Debug.Print Counter
Counter = Counter + 1
Loop
Do Until/loop
Counter = 1
Do Until Counter > 1000
Debug.Print Counter
Counter = Counter + 1
Loop
• This loop repeats Until the Counter variable
exceeds 1000. Note a DoUntil/Loop structure
will not be entered if the Until condition is
already True on the first encounter.
Do/Loop While
Do/Loop While Example:
Sum = 1
Do
Debug.Print Sum
Sum = Sum + 3
Loop While Sum <= 50