VB.Net学习笔记之循环语句的详细介绍
循环语句
VB.Net中的循环语句分为:Do While Loop、For Next、For Each三种。
Do While Loop
Do While Loop有三种形式,这系列的循环是用于预先不知道循环的上限时使用的。在使用Do While Loop语句时要注意,因为它们是不确定循环次数,所以要小心不要造成死循环。
Do While Loop举例 |
Public Class TestA
Public Sub New()
Dim i As Int32
i = 1
Do While i < 100 '先判断后执行
i += 1
Exit Do
Loop
i = 1
Do
i += 1
Exit Do
Loop While i < 100 '先执行后判断
While i < 100 'Do While i < 100
i += 1
Exit While
End While
End Sub End Class |
For Next
和Do While Loop不一样,For Next是界限循环。For 语句指定循环控制变量、下限、上限和可选的步长值。
For Next举例 |
Public Class TestA
Public Sub New()
Dim i As Int32
For i = 0 To 100 Step 2
Next i
End Sub End Class |
For Each
For Each也是不定量循环, For Each是对于集合中的每个元素进行遍历。如果你需要对一个对象集合进行遍历,那就应该使用For Each。
For Each举例 |
Public Class TestA
Public Sub New()
Dim Found As Boolean = False
Dim MyCollection As New Collection
For Each MyObject As Object In MyCollection
If MyObject.Text = "Hello" Then
Found = True
Exit For
End If
Next
End Sub End Class |
本文地址:http://www.45fan.com/dnjc/70532.html