Home » F# for to do Example

F# for to do Example

by Online Tutorials Library

F# For Loop

The F# for loop is used to iterate a part of program several times. In F#, it is recommended to use for loop than while or do-while loops, if the number of iteration is fixed.

There are 3 types of for loop in F#.

  • for-to-do loop
  • for-downto-do loop
  • for-in-do loop

The syntax of F# language for loop is given below:

F# For-To-Do Example

In F#, for-to-do loop follows incremental approach. It increments after each iteration. It starts from start value and iterates till finish value. Let’s see an example.

Output:

1  2  3  4  5  6  7  8  9  10  

F# Nested For-To-Do Loop

In F#, we can use for loop inside another for loop. It is known as nested for loop. The inner loop is executed fully when outer loop is executed one time. So if outer and inner loop are executed 3 times, inner loop will be executed 3 times for each outer loop i.e. total 9 times.

Let’s see a simple example of nested for loop in F#.

Output:

1 1  1 2  1 3  2 1  2 2  2 3  3 1  3 2  3 3  

You may also like