VB.Net - Continue 语句



Continue 语句会导致循环跳过其主体剩余部分,并在立即重新迭代之前重新测试其条件。它的工作方式有点像 Exit 语句。它不是强制终止,而是强制执行循环的下一个迭代,跳过中间的任何代码。

对于 For...Next 循环,Continue 语句会导致循环执行条件测试和增量部分。对于 While 和 Do...While 循环,continue 语句会导致程序控制传递到条件测试。

语法

Continue 语句的语法如下:

Continue { Do | For | While }

流程图

VB.Net continue statement

示例

Module loops
   Sub Main()
      ' local variable definition 
      Dim a As Integer = 10
      Do
         If (a = 15) Then
            ' skip the iteration '
            a = a + 1
            Continue Do
         End If
         Console.WriteLine("value of a: {0}", a)
         a = a + 1
      Loop While (a < 20)
      Console.ReadLine()
   End Sub
End Module

当以上代码编译并执行时,会产生以下结果:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
vb.net_loops.htm
广告

© . All rights reserved.