There is also a while statement in F #. Next we're going to try to write code using the while statement.
In the article "invariance", some people commented that ref can be applied and we tried to use it.
The scope of the while loop is represented by indentation.
let num = ref 0
while !num < 10 do
num := !num + 1
printfn "%d" !num
Ref is a keyword that represents a reference. It would be nice to think of it as a pointer to the C language. num is a pointer variable, keeping a reference to the value 0. Use when taking the value! Operator. To use when assigning value: =. This is Pascal and similar.
The code above is the ordinal representation of 1 to 10. But the readability is poor. I think it's possible that the while loop is rarely used in F #.
The code for the same functionality overridden by the For statement is as follows:
for num = 1 to 10 do
printfn "%d" num
The following code is OK as well:
for num in 1..10 do
printfn "%d" num
The For statement is a process, which is important when used.
But if you want to write a solution from 1 to 10 code, the for statement is not. This is, of course, in cases where ref or mutable are not used.
About this and listen to let's.