它为集合中的每个元素重复一组语句。 此循环用于访问和操作数组或VB.Net集合中的所有元素。
此循环结构的语法是 -
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