while
loop is a basic loop provided by language that executes a set of code statements until conditional_expression is met.
syntax
while conditional_expression do
// body code statements
conditional_expression is a valid expression, that evaluates to a boolean value. if it is true, It executes the body. body code is always evaluated to the unit and does not return any value.
mutable Variable is declared with 0.
Checking number is less than 5 and inside a body print number, increment the number by assigning using <-
Here is an example
let mutable number = 0
while (number < 5) do
printfn "%d" number
number <- number + 1
Here is an example of an infinite loop with a while loop
The condition is true, the code inside is always executed.
while true do
printfn "infinite loop"
How to break from while doing the loop in F#?
F# has no break
feature to exit from the while loop.
Alternatively, You can do using making a mutable conditional expression to false
inside a body.
This is an ugly way of exiting from a body inside while loop
let mutable isExit = false
let breakExpression=true;
while not isExit do
printfn "Inside Body" ;
if breakExpression then
isExit <- true
done
If you want a clean approach, write a recursion function that gets an exit at end interaction.