1、For Each... Next 语句通常用于扫描数组或集合中的所有元素,其一般语法格式如下:
For Each 变量 In 数组名或集合名
。。。。。。。。
。。。。。。。。。。。。。
Next
声明一个字符串数组starr,并初始化,用 For Each Next 语句在其中找“星期三”的元素
例:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim starr() As String = {"星期一", "星期二", "星期三", "星期四"}
For Each temp As String In starr
If temp = "星期三" Then
MsgBox("成功找到星期三")
Exit For
End If
Next
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim starr() As String = {"星期一", "星期二", "星期三", "星期四"}
For Each temp As String In starr
MsgBox(temp)
Next
MsgBox("for each 循环完毕")
End Sub
End Class