表达式 while...do
用于在指定的测试条件为 true 时执行迭代执行(循环)。
语法
while test-expression do
body-expression
注解
将计算 测试表达式 ; true
如果是,则执行 正文表达式 ,并再次计算测试表达式。
正文表达式必须具有类型unit
。 如果测试表达式为 false
,则迭代结束。
下面的示例演示了 while...do
表达式的使用。
open System
let lookForValue value maxValue =
let mutable continueLooping = true
let randomNumberGenerator = new Random()
while continueLooping do
// Generate a random number between 1 and maxValue.
let rand = randomNumberGenerator.Next(maxValue)
printf "%d " rand
if rand = value then
printfn "\nFound a %d!" value
continueLooping <- false
lookForValue 10 20
上一个代码的输出是介于 1 和 20 之间的随机数流,最后一个是 10。
13 19 8 18 16 2 10
Found a 10!
注释
可以在序列表达式和其他计算表达式中使用 while...do
,在这种情况下,将使用自定义版本的 while...do
表达式。 有关详细信息,请参阅 序列、 异步表达式、 任务表达式和 计算表达式。