F# - while..do Loops


The while...do expression is used to perform iterative execution while a specified test condition is true.


while test-expression do

The test-expression is evaluated first; if it is true, the body-expression is executed and the test expression is evaluated again. The body-expression must have type unit, i.e., it should not return any value. If the test expression is false, the iteration ends.


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

When you compile and execute the program, it yields the following output −

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