Visual Basic Each…Next 循环

Each…Next 为集合中的每个元素重复执行一组语句,可简单理解为遍历数组元素。此循环用于访问和操作数组或 VB 集合中的所有元素。

这个循环结构的语法是:

  1. For Each element [ As datatype ] In group
  2. [ statements ]
  3. [ Continue For ]
  4. [ statements ]
  5. [ Exit For ]
  6. [ statements ]
  7. Next [ element ]

实例

  1. Module loops
  2. Sub Main()
  3. Dim anArray() As Integer = {1, 3, 5, 7, 9}
  4. Dim arrayItem As Integer
  5. 'displaying the values
  6. For Each arrayItem In anArray
  7. Console.WriteLine(arrayItem)
  8. Next
  9. Console.ReadLine()
  10. End Sub
  11. End Module

结果如下:

  1. 1
  2. 3
  3. 5
  4. 7
  5. 9

分类导航