Home

Conditional Looping Statements: For

   

For Looping to an End

Looping consists of visiting each element in a list or a group of items, from a starting point to an ending point. There are two types of loops you will use. The most fundamental looping consists of counting values from one starting point to an ending value. To support this operation, the F# language provides some keywords used in a formula as follows:

for identifier = start [ to | downto ] end do
   body

The for, the to, and the do keywords are required. The identifier is a local name that is the number that is currently used. start is the value from where to start counting. end is the value where to end counting.

The body of the looping operation starts with the do keyword that is followed by the body of the loop. It could be on the same line or on the next line. That body can contain any code you see fit. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text   <- "Numbers"
exercise.Width  <- 135
exercise.Height <- 190

let lbxNumbers = new ListBox()
lbxNumbers.Left   <- 21
lbxNumbers.Top    <- 12
lbxNumbers.Width  <-  80
lbxNumbers.Height <- 140

for number = 1 to 10 do
    lbxNumbers.Items.Add(string number) |> ignore

exercise.Controls.Add lbxNumbers

Application.Run exercise

This would produce:

For Looping to an End

The start and end values can also come from previously declared variables.

The to keyword is used if you want to count from one lower value, such as 0 or 1, to a higher value, such as 5. The downto keyword is used if you want to count backward, such from 75 to 42. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text   <- "Numbers"
exercise.Width  <- 135
exercise.Height <- 190

let lbxNumbers = new ListBox()
lbxNumbers.Left   <- 21
lbxNumbers.Top    <- 12
lbxNumbers.Width  <-  80
lbxNumbers.Height <- 140

for number = 75 downto 42 do
    lbxNumbers.Items.Add(string number) |> ignore

exercise.Controls.Add lbxNumbers

Application.Run exercise

This would produce:

For Looping to an End For Looping to an End For Looping to an End
     
 

For Looping in a Range

Instead of counting numbers, you may have a collection and want to visit each one of its items. To support this, the F# language supports another looping technique whose formula is:

for identifier in range do
   body

In this case, the range is the collection that holds the values. You can create the collection directly in the loop. The formula of the range is:

starting-value .. ending-value

Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text   <- "Exercise"
exercise.Width  <- 135
exercise.Height <- 160

let lbxNumbers = new ListBox()
lbxNumbers.Left   <- 21
lbxNumbers.Top    <- 12
lbxNumbers.Width  <-  80
lbxNumbers.Height <- 100

for number in 5 .. 10 do
    lbxNumbers.Items.Add(string number) |> ignore

exercise.Controls.Add lbxNumbers

Application.Run exercise

This would produce:

For Looping in a Range

You can also first store the range in a variable and then use that variable in the loop.

   
 

Home Copyright © 2015, FunctionX Home