Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering F#

You're reading from   Mastering F# A comprehensive and in-depth guide to writing functional programs using F#

Arrow left icon
Product type Paperback
Published in Nov 2016
Publisher
ISBN-13 9781784393434
Length 264 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Suhaib Fahad Suhaib Fahad
Author Profile Icon Suhaib Fahad
Suhaib Fahad
Alfonso García-Caro Núñez Alfonso García-Caro Núñez
Author Profile Icon Alfonso García-Caro Núñez
Alfonso García-Caro Núñez
Arrow right icon
View More author details
Toc

Control structures

Control structures refer to the decisions made that affect the order of execution of the code. Although F# is an expression-based language, it also provides control flow constructs common of statement-based languages, such as looping, that allows us to write code with side-effects. We will be discussing this, as well as how to express conditions, in the following sections.

Looping

There are two types of loops: the for loop and the while loop. The For loops have the following two types of expression:

  • for...to: This iterates over a range of values
  • for...in: This is more like the foreach loop in other .NET languages as it loops over an enumerable collection

The for...to expression

The for...to expression loops/iterates over a range of values. The range can be forward/reverse or generated via a function. The return of this expression is unit type.

The forward expression works by incrementing the following values in the loop:

// A simple for...to loop.  
let function1...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime