ໃນ ພາສາ Visual Basic 6 ມີຮູບແບບການ Loop ຢູ່ 5 ແບບ
1. ແບບ Do Loop
ແບບ Do Loop statements ມີ 4 ແບບ ທີ່ຕ່າງກັນກັນ:
a)
Do While condition Block of one or more VB statements Loop
b)
Do Block of one or more VB statements Loop While condition
c)
Do Until condition Block of one or more VB statements Loop
d)
Do Block of one or more VB statements Loop Until condition
Do while counter <=1000 num.Text=counter counter =counter+1 Loop * The above example will keep on adding until counter > 1000 The above example can be rewritten as Do counter=counter+1 Loop until counter>1000
ຖ້າຕ້ອງການອອກຈາກ do ແມ່ນ ໃສ່ຄຳສັ່ງ exit do
ຕົວຢ່າງໃຊ້ exit do
Dim sum, n As Integer Private Sub Form_Activate() List1.AddItem "n" & vbTab & "sum" Do n = n + 1 Sum = Sum + n List1.AddItem n & vbTab & Sum If n = 100 Then Exit Do End If Loop End Sub
2. For….Next Loop
For counter=startNumber to endNumber (Step increment) One or more VB statements Next
ຕົວຢ່າງ:
For counter=1 to 1000 step 10 counter=counter+1 Next
ຖ້າຈະອອກຈາກ loop for ແມ່ນໃຊ້ Exit For
ຕົວຢ່າງ:
Private Sub Form_Activate( ) For n=1 to 10 If n>6 then Exit For End If Else Print n End If End Sub
3. ການຊ້ອນກັນ For…Next Loop
ແບບໂຄດ
The Structure of a nested loop is : For counter1=startNumber to endNumber (Step increment) For counter2=startNumber to endNumber (Step increment) One or more VB statements Next counter2 Next counter1
ຕົວຢ່າງ:
Private Sub Form_Activate ( ) For firstCounter= 1to 5 Print "Hello" For secondCounter=1 to 4 Print "Welcome to the VB tutorial" Next secondCounter Next firstCounter Print"Thank you" End Sub
ຜົນຮັບ
Image does not exist: https://image.ibb.co/mE7EWv/loopvb6.jpg
4. ການໃຊ້ While….Wend Loop
ແບບໂຄດ
While condition Statements Wend
ຕົວຢ່າງ
Dim sum, n As Integer Private Sub Form_Activate() List1.AddItem "n" & vbTab & "sum" While n <> 100 n = n + 1 Sum = Sum + n List1.AddItem n & vbTab & Sum Wend End Sub