Visual Basics : (Lecture 6)
Visual Basics : (Lecture 6)
[Lecture 6 ]
Bhumika Shah
{Lecturer, Computer Engineering Department }
Select Case Decision Structure..
A Select Case structure begins with the Select Case keywords
and ends with the End Select keywords.
End Sub
Complex FOR…NEXT Loops..
Creating Loops with a Custom Counter…
You can create a loop with a counter pattern other than 1, 2, 3, 4,
and so on.
First, you specify a different start value in the loop.
Then, you use the Step keyword to increment the counter at different
intervals.
For example, following loop controls the print sequence of numbers
on a form:
5
10
15
20
25
……
100
Private Sub Command1_Click()
For i = 5 To 100 Step 5
Print i
Next i
End Sub
Specifying Decimal Values…
1
1.5
2
2.5
Nested For...Next Loops ::
Private Sub Command1_Click()
For i = 1 To 5
For j = 1 To 5
Print i + j
Next j
Next I
End Sub
Using the EXIT FOR Statement…
In Visual Basic, you can use an Exit For statement to exit
a For...Next loop before the loop has finished executing.
Private Sub Command1_Click()
For i = 1 To 5
For j = 1 To 5
If i + j = 8 Then Exit For
Print i + j
Next j
Next i
End Sub
Writing Do Loops…
Do loops are valuable because occasionally
you can’t know in advance how many times a
loop should repeat.
As an alternative to a For...Next loop, you can
Do While condition
block of statements to be executed
Loop
Private Sub Command1_Click()
i=0
Do While i < 25
'block of statements to be executed
Print "value is :: "; i
i=i+1
Loop
End Sub
NOTE ::
This code brings up an interesting fact about Do
Loops:
if the condition at the top of the loop Is
not True when the Do statement is first evaluated,
Visual Basic never executes the Do loop.
Conditional Test at the Bottom of the
Loop …
If you want the loop to always run at least once in a
program, put the conditional test at the bottom of
the loop.
Private Sub Command1_Click()
i=0
Do
Print "value is :: "; i
i=i+1
Loop While i < 25
End Sub
The advantage of this syntax is that you can update the