只要给定的条件为真(True
),它就会执行一系列语句。
这个循环结构的语法是:
While condition
[ statements ]
[ Continue While ]
[ statements ]
[ Exit While ]
[ statements ]
End While
在这里,语句(statements)可以是单个或一组语句。 条件(condition)可能是任何表达式,而true
表示逻辑为真。当条件成立时,循环迭代。
当条件变为false
时,程序控制传递到循环之后的行。
流程图
在这里,While
循环的关键是循环可能永远不会运行。当条件被测试并且结果为false
时,循环体将被跳过,while
循环之后的第一条语句将被执行。
示例
Module loops
Sub Main()
Dim a As Integer = 10
' while loop execution '
While a < 20
Console.WriteLine("value of a: {0}", a)
a = a + 1
End While
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: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19