Home
F#
for Loop Examples (while)
Updated Dec 22, 2023
Dot Net Perls
For. In F# a loop continues forward—we implement loops with for and while. Loops are powerful, but functional-based designs are usually emphasized in this language.
In this language, declarative constructs are often preferred. But for getting things done, loops are often a good choice—and they can be rewritten later if needed.
Loop, String Chars
For example. This program shows 2 for-loops, and both of the constructs loop over the same values. In programs, either form can be used.
Start The first for-loop uses an iteration variable called "i" and goes from 0 through 3 inclusive.
Next This loop uses range syntax. The two periods are part of an inclusive range of 0, 1, 2 and 3.
// Loop from 0 through 3 inclusive. // ... The variable i is incremented by 1 each time. for i = 0 to 3 do printfn "For, to: %A" i // This is the same loop but with a range. for i in 0 .. 3 do printfn "For, in: %A" i
For, to: 0 For, to: 1 For, to: 2 For, to: 3 For, in: 0 For, in: 1 For, in: 2 For, in: 3
For-in, list. A for-in loop enumerates over each element in a collection (like a list). Here we create a list of ints called "codes."
List
Note The for-in loop accesses all elements in the list. It provides no indexes, so the syntax is simpler.
let codes = [3; 12; 34; 3] // Loop over ints from list. for code in codes do // If code is 3 then write a special word. if (code = 3) then printfn "Three" // Write value of int from list. printfn "%A" code
Three 3 12 34 Three 3
Downto. With this keyword we create a decrementing loop. After each iteration, the iteration variable (in this program "i") is reduced by 1.
// Specify a decrement for-loop with downto. for i = 10 downto 6 do printfn "%A" i
10 9 8 7 6
While. The most versatile loop in F# is the while-loop. Here we can increment, decrement, or loop forever. We must be careful not to make mistakes.
Info The iteration variable used as a counter in a while-loop should be marked as mutable so we can change it.
Tip We decrement the variable "x" by assigning it the current value minus 1.
// Use an iteration variable that starts at 4. // ... The variable must be mutable. let mutable x = 4 // Continue looping while x is positive. while x >= 0 do // Write value. printfn "%A" x // Decrement x. x <- x - 1
4 3 2 1 0
A review. In functional programs, recursion and method-based iteration is often preferred. But loops are still wonderful. In F# we can use iteration and increment variables.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 22, 2023 (edit link).
Home
Changes
© 2007-2025 Sam Allen