F# - while..do 循环



while...do 表达式用来在指定的测试条件为真时执行迭代执行。

语法

while test-expression do
   body-expression

首先计算测试表达式;如果结果为真,则执行主体表达式并再次计算测试表达式。主体表达式必须为 unit 类型,即它不应该返回任何值。如果测试表达式为假,则迭代结束。

示例

let mutable a = 10
while (a < 20) do
   printfn "value of a: %d" a
   a <- a + 1

当您编译并执行该程序时,它将生成如下输出 −

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
fsharp_loops.htm
广告
© . All rights reserved.