VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • For...Next 语句 (Visual Basic)

将一组语句重复执行指定的次数。

For counter [ As datatype ] = start To end [ Step step ]
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ counter ]

部件

组成部分

说明

counter

For 语句的必选项。 数值变量。 它是循环的控制变量。

datatype

如果尚未声明 counter,则是必选项。 counter 的数据类型。

start

必选。 数值表达式。 counter 的初始值。

end

必选。 数值表达式。 counter 的最终值。

step

可选。 数值表达式。 每次循环后 counter 的增量。

statements

可选。 放在 For 和 Next 之间的一条或多条语句,它们将运行指定的次数。

Continue For

可选。 将控制权交给下一轮循环迭代。

Exit For

可选。 将控制转移到 For 循环外。

Next

必选。 结束 For 循环的定义。

 提示

To 关键字在此处用于指定计数器的范围。 它还用于在 Select...Case 语句 (Visual Basic) 和数组声明中指定值的范围。 有关数组声明的更多信息,请参见 Dim 语句 (Visual Basic)。

备注

当需要将一组语句重复执行设置好的次数时,请使用 For...Next 结构。

当 For...Next 循环开始时,Visual Basic 将计算 start、end 和 step。 这是它唯一一次计算这些值。 然后将 start 赋予 counter。 运行语句块之前,它先将 counter 与 end 进行比较。 如果 counter 已大于(或在 step 为负的情况下小于)end 值,则 For 循环终止,并将控制将传递给 Next 语句后面的语句。 否则,将运行语句块。

每次 Visual Basic 遇到 Next 语句时,都按 step 递增 counter,然后返回到 For 语句。 它再次将 counter 与 end 进行比较,并再次根据结果运行块或者退出循环。 这一过程将一直持续下去,直到 counter 传递 end 或者遇到 Exit For 语句为止。

在 counter 传递 end 之后,循环才会停止。 如果 counter 等于 end,则循环继续。 如果 step 为正数,确定是否运行循环代码块的比较运算将为 counter <= end;如果 step 为负数,则为 counter >= end。

如果在循环内更改 counter 的值,将会使代码的阅读和调试变得更加困难。 更改 start、end 或 step 的值不会影响首次进入循环时所确定的迭代值。

 提示

当事先不知道需要运行多少次循环中的语句时,While...End While 语句 (Visual Basic) 或 Do...Loop 语句 (Visual Basic) 循环可很好地发挥作用。 但是,如果您希望让循环运行特定次数,则 For...Next 是较好的选择。 您需要在第一次输入循环时确定迭代次数。

步骤参数

step 的值可以是正数或负数。 它将按以下方式决定循环处理过程:

Step 值

循环执行的条件

正数或零

counter <= end

counter >= end

如果没有指定,则 step 的默认值为 1。

计数器参数

如果未在此循环外声明 counter,则必须在 For 语句中声明它。 这种情况下,counter 的范围就是循环的主体。 但是,不能同时在循环内外声明 counter。

可以选择在 Next 语句中指定 counter。 这将提高程序的可读性,尤其是在具有嵌套的 For 循环的情况下。 必须指定与相应的 For 语句中出现的变量相同的变量。

counter 的数据类型确定迭代的类型,并且必须属于以下类型之一:

  • Byte、SByte、UShort、Short、UInteger、Integer、ULong、Long、Decimal、Single 或 Double。

  • 您使用 Enum 语句 (Visual Basic) 声明的枚举。

  • 一个 Object。

  • 具有下列运算符的 T,其中 B 是可以在 Boolean 表达式中使用的类型。

    Public Shared Operator >= (op1 As T, op2 As T) As B

    Public Shared Operator <= (op1 As T, op2 As T) As B

    Public Shared Operator - (op1 As T, op2 As T) As T

    Public Shared Operator + (op1 As T, op2 As T) As T

start、end 和 step 表达式可以计算为拓宽到 counter 类型的任何数据类型。 如果要将用户定义的类型用于 counter,这意味着您可能必须定义 CType 转换运算符,来将 start、end 或 step 的类型转换为 counter 的类型。

嵌套循环

可以将一个循环放在另一个循环内以嵌套 For 循环。 不过,每个循环必须具有唯一的 counter 变量。

您还可以将多个不同类型的控制结构相互进行嵌套。 有关更多信息,请参见 嵌套的控件结构 (Visual Basic)。

如果先遇到外部嵌套级别的 Next 语句,后遇到内部嵌套级别的 Next 语句,编译器将发出错误信号。 不过,仅当在所有 Next 语句中都指定了 counter 时,编译器才能检测到这种重叠错误。

退出 For

Exit 语句 (Visual Basic) 立即退出 For…Next 循环,并将控制权转交给 Next 语句之后的语句。

可以在 For…Next 循环中放置任意数量的 Exit For 语句。 当在嵌套的 For…Next 循环内使用时,Exit For 将退出最内层的循环,并将控制权交给下一层较高级别的嵌套。

Exit For 通常在计算特定条件后使用,例如在 If...Then...Else 结构中。 您可能希望针对下列条件使用 Exit For:

  • 继续循环不必要或不可能。 这可能是由错误的值或终止请求引起的。

  • 在 Try...Catch...Finally 中捕获异常。 可以在 Finally 块的末尾使用 Exit For。

  • 存在无限循环,这种循环可能会运行很多次甚至无限次。 如果检测到这样的条件,就可以使用 Exit For 退出循环。 有关更多信息,请参见 Do...Loop 语句 (Visual Basic)。

Continue For 语句将控制权立即转移给下一轮循环。 有关更多信息,请参见 Continue 语句 (Visual Basic)。

示例

下面的示例阐释了 For…Next 语句的用法。 循环计数器变量在每次循环迭代时都会递增。 未指定 step 参数,因此其默认值为 1。

VB
For index As Integer = 1 To 5
    Debug.Write(index.ToString & " ")
Next
Debug.WriteLine("")
' Output: 1 2 3 4 5

在下面的示例中,counter 和 step 参数是 Double 浮点数。

VB
For number As Double = 2 To 0 Step -0.25
    Debug.Write(number.ToString & " ")
Next
Debug.WriteLine("")
' Output: 2 1.75 1.5 1.25 1 0.75 0.5 0.25 0 

下面的示例演示了具有不同步长值的 For...Next 嵌套结构。 外部循环为每次循环迭代创建一个字符串。 内部循环减小每次循环迭代的循环计数器变量。

VB
For indexA = 1 To 3
    ' Create a new StringBuilder, which is used
    ' to efficiently build strings.
    Dim sb As New System.Text.StringBuilder()

    ' Append to the StringBuilder every third number
    ' from 20 to 1 descending.
    For indexB = 20 To 1 Step -3
        sb.Append(indexB.ToString)
        sb.Append(" ")
    Next indexB

    ' Display the line.
    Debug.WriteLine(sb.ToString)
Next indexA
' Output:
'  20 17 14 11 8 5 2
'  20 17 14 11 8 5 2
'  20 17 14 11 8 5 2

下面的示例从泛型列表中删除所有元素。 而不是使用按降序顺序循环的 For Each...Next 语句 (Visual Basic) ForNext 语句。 这是因为 removeAt 方法会导致被删除元素之后的元素具有较小的索引值。

VB
Dim lst As New List(Of Integer) From {10, 20, 30, 40}

For index As Integer = lst.Count - 1 To 0 Step -1
    lst.RemoveAt(index)
Next

Debug.WriteLine(lst.Count.ToString)
' Output: 0

下面的示例阐释了 Continue For and Exit For 语句的用法。

VB
For index As Integer = 1 To 100000
    ' If index is between 5 and 7, continue
    ' with the next iteration.
    If index >= 5 And index <= 8 Then
        Continue For
    End If

    ' Display the index.
    Debug.Write(index.ToString & " ")

    ' If index is 10, exit the loop.
    If index = 10 Then
        Exit For
    End If
Next
Debug.WriteLine("")
' Output: 1 2 3 4 9 10

下面的示例循环访问通过使用 Enum 语句 (Visual Basic) 声明的枚举。

VB
Public Enum Mammals
    Buffalo
    Gazelle
    Mongoose
    Rhinocerous
    Whale
End Enum


Public Sub ListSomeMammals()
    For mammal As Mammals = Mammals.Gazelle To Mammals.Rhinocerous
        Debug.Write(mammal.ToString & " ")
    Next
    Debug.WriteLine("")
    ' Output: Gazelle Mongoose Rhinocerous
End Sub

在下面的示例中,语句参数使用了具有 +、 -、>= 和 <= 运算符的运算符重载的类。

VB
Private Class Distance
    Public Property Number() As Double

    Public Sub New(ByVal number As Double)
        Me.Number = number
    End Sub

    ' Define operator overloads to support For...Next statements.
    Public Shared Operator +(ByVal op1 As Distance, ByVal op2 As Distance) As Distance
        Return New Distance(op1.Number + op2.Number)
    End Operator

    Public Shared Operator -(ByVal op1 As Distance, ByVal op2 As Distance) As Distance
        Return New Distance(op1.Number - op2.Number)
    End Operator

    Public Shared Operator >=(ByVal op1 As Distance, ByVal op2 As Distance) As Boolean
        Return (op1.Number >= op2.Number)
    End Operator

    Public Shared Operator <=(ByVal op1 As Distance, ByVal op2 As Distance) As Boolean
        Return (op1.Number <= op2.Number)
    End Operator
End Class


Public Sub ListDistances()
    Dim distFrom As New Distance(10)
    Dim distTo As New Distance(25)
    Dim distStep As New Distance(4)

    For dist As Distance = distFrom To distTo Step distStep
        Debug.Write(dist.Number.ToString & " ")
    Next
    Debug.WriteLine("")

    ' Output: 10 14 18 22 
End Sub

 

原文链接:https://docs.microsoft.com/zh-cn/previous-versions/visualstudio/visual-studio-2010/5z06z1kb(v=vs.100)


相关教程