vb.net中的循环语句
1、for next 循环语句
2、'Declare variable
Dim intCount As Integer
'Clear the list
ClearList() '调用其他程序
'Perform a loop
For intCount = 1 To 5
'Add the item to the list
lstData.Items.Add("I'm item " & intCount.ToString &
" in the list!")
Next
End Sub
Private Sub ClearList()
'Clear the list
lstData.Items.Clear()
End Sub
3、执行结果如下图:

4、使用step 控制步长循环间隔
Private Sub btnForNextLoopWithStep_Click(sender As Object,
e As EventArgs) Handles btnForNextLoopWithStep.Click
'Clear the list
ClearList()
'Perform a loop
For intCount As Integer = 4 To 62 Step 7
'Add the item to the list
lstData.Items.Add(intCount.ToString)
Next
End Sub
5、执行结果如下图所示

6、for next 循环,步长为负,循环控制
7、Private Sub btnBackwardsForNextLoop_Click(sender As Object,
e As EventArgs) Handles btnBackwardsForNextLoop.Click
'Clear the list
ClearList()
'Perform a loop
For intCount As Integer = 10 To 1 Step -1
'Add the item to the list
lstData.Items.Add(intCount.ToString)
Next
End Sub
8、执行结果如下图所示:

1、For Each…Next 循环语句,控制流程介绍示例代码如下:
2、Private Sub btnForEachLoop_Click(sender As Object,
e As EventArgs) Handles btnForEachLoop.Click
'Clear the list
ClearList()
'List each folder at the root of your C drive
For Each strFolder As String In
My.Computer.FileSystem.GetDirectories("C:\")
'Add the item to the list
lstData.Items.Add(strFolder)
Next
End Sub
1、do until ... loop 循环,示例代码如下:
2、Private Sub btnDoUntilLoop_Click(sender As Object,
e As EventArgs) Handles btnDoUntilLoop.Click
'Declare variables
Dim objRandom As New Random
Dim intRandomNumber As Integer = 0
'Clear the list
ClearList()
'Process the loop until intRandomNumber = 10
Do Until intRandomNumber = 10
'Get a random number between 0 and 24
intRandomNumber = objRandom.Next(25)
'Add the number to the list
lstData.Items.Add(intRandomNumber.ToString)
Loop
End Sub
3、执行结果如下图所示:

1、示例代码如下:
2、Private Sub btnDoWhileLoop_Click(sender As Object,
e As EventArgs) Handles btnDoWhileLoop.Click
'Declare variables
Dim objRandom As New Random
Dim intRandomNumber As Integer = 0
'Clear the list
ClearList()
'Process the loop while intRandomNumber < 15
Do While intRandomNumber < 15
'Get a random number between 0 and 24
intRandomNumber = objRandom.Next(25)
'Add the number to the list
lstData.Items.Add(intRandomNumber.ToString)
Loop
End Sub
3、执行结果如下图所示;

1、示例代码如下:
2、Private Sub btnNestedLoops_Click(sender As Object,
e As EventArgs) Handles btnNestedLoops.Click
'Clear the list
ClearList()
'Process an outer loop
For intOuterLoop As Integer = 1 To 2
'Process a nested (inner) loop
For intInnerLoop As Integer = 1 To 3
lstData.Items.Add(intOuterLoop.ToString &
", " & intInnerLoop.ToString)
Next
Next
End Sub
3、执行结果如下图所示:
