F# - while..do Loops



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

Syntax

while test-expression do
   body-expression

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.

Example

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
fsharp_loops.htm
Advertisements