VB.Net允许在另一个循环内使用一个循环。 以下部分显示了几个例子来说明这个概念。
For counter1 [ As datatype1 ] = start1 To end1 [ Step step1 ] For counter2 [ As datatype2 ] = start2 To end2 [ Step step2 ] ... Next [ counter2 ] Next [ counter 1]
VB.Net中嵌套While循环语句的语法如下:
While condition1 While condition2 ... End While End While
VB.Net中嵌套Do
... While循环语句的语法如下:
Do { While | Until } condition1 Do { While | Until } condition2 ... Loop Loop
关于循环嵌套的最后一点是,你可以把任何类型的循环放在任何其他类型的循环里面。
例如,for循环可以在while循环内,反之亦然。
Dim i, j As Integer For i = 2 To 100 For j = 2 To i ' if factor found, not prime If ((i Mod j) = 0) Then Exit For End If Next j If (j > (i \ j)) Then Proj.MsgDebug.Add("{0} is prime", i) End If Next i '返回结果:2 Is prime '返回结果:3 Is prime '返回结果:5 Is prime '返回结果:7 Is prime '返回结果:11 Is prime '返回结果:13 Is prime '返回结果:17 Is prime '返回结果:19 Is prime '返回结果:23 Is prime '返回结果:29 Is prime '返回结果:31 Is prime '返回结果:37 Is prime '返回结果:41 Is prime '返回结果:43 Is prime '返回结果:47 Is prime '返回结果:53 Is prime '返回结果:59 Is prime '返回结果:61 Is prime '返回结果:67 Is prime '返回结果:71 Is prime '返回结果:73 Is prime '返回结果:79 Is prime '返回结果:83 Is prime '返回结果:89 Is prime '返回结果:97 Is prime |