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

简单的语句介绍,我们就到这里了,其他语句在以后对 VB.Net 的逐步深入中,我们会一一阐述。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus