F# - Nested Loops


Advertisements

F# programming language allows to use one loop inside another loop.

Syntax

The syntax for a nested for loop statement could be as follows −

for var1 = start-expr1 to end-expr1 do
   for var2 = start-expr2 to end-expr2 do
      ... // loop body

The syntax for a nested while loop statement could be as follows −

while test-expression1 do
   while test-expression2 do
      body-expression

Example

let main() =
   for i = 1 to 5 do
      printf "\n"
      for j = 1 to 3 do
         printf "*"
main()

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

***
***
***
***
***
fsharp_loops.htm
Advertisements