Home
Search
For and While Loop ExamplesLoop over ranges and collections with the for-to, for-in and while loops.
F#
This page was last reviewed on Sep 30, 2022.
For, while. 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 hard to beat—and they can be rewritten later if needed.
For example. This program shows 2 for-loops. Both of the constructs loop over the same values. In programs, either form can be used.
First The first for-loop uses an iteration variable called "i" and goes from 0 through 3 inclusive.
Second 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.
Mutable The iteration variable used as a counter in a while-loop should be marked as mutable so we can change it.
Decrement 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 30, 2022 (grammar).
Home
© 2007-2022 sam allen.
see site info on the changelog.