Do语句循环
Do语句循环有两种结构,第一种是循环条件在前。先来看看Do-While语句,当满足循环条件的时候会继续循环,不满足条件时跳出循环。这里顺便提一下,VB中没有块注释,只有单行注释,以单引号开头,直到句末。
'Do While语句 Dim i = 0 Do While i < 5 If i = 3 Then Exit Do End If Console.Write(i) i += 1 Loop
再来看看Do-Until语句,和Do-While相反,在不满足条件的时候会执行循环,满足条件时跳出循环。
'Do Until语句 i = 0 Do Until i > 5 If i < 3 Then i += 2 Continue Do End If Console.Write(i) i += 1 Loop
Do循环的第二种形式就是先执行循环体,然后进行判断,同样有While和Until两种。
i = 0 Do Console.Write(i) i += 1 Loop While i < 5 Console.WriteLine
While循环
还有就是传统的While循环。
'While循环 i = 0 While i < 5 Console.Write(i) i += 1 End While
For循环
下面是For循环的例子,在For循环中可以使用Step指定步长。
For counter As Integer = 1 To 9 Step 1 Console.Write(counter) Next
Foreach循环
Foreach循环用于迭代一个列表中的每一项。
Dim array() as Integer = {1, 2, 3, 4, 5, 6, 7} For Each item As Integer In array Console.Write(item) Next