Visual Basic Each…Next 循环
Each…Next 为集合中的每个元素重复执行一组语句,可简单理解为遍历数组元素。此循环用于访问和操作数组或 VB 集合中的所有元素。
这个循环结构的语法是:
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
实例
Module loops
Sub Main()
Dim anArray() As Integer = {1, 3, 5, 7, 9}
Dim arrayItem As Integer
'displaying the values
For Each arrayItem In anArray
Console.WriteLine(arrayItem)
Next
Console.ReadLine()
End Sub
End Module
结果如下:
1
3
5
7
9