循環語句
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
簡單的語句介紹,我們就到這里了,其他語句在以后對VB.Net的逐步深
|