F# conditionals
last modified December 2, 2026
In this article we show how to run code only when a condition holds, with
the if, then, elif and
else keywords.
In many languages if is a statement. In F# it is an
expression: it has a value and a type, like 2 + 3.
This decides what the branches may contain, as the later sections show.
Choosing between many cases by their shape is done with the
match expression, covered in
F# match expression.
The if expression
The simplest conditional has a condition after if and a block
after then. The block runs only when the condition is
true.
let temperature = 28
if temperature > 25 then
printfn "It is a hot day"
printfn "Temperature: %d °C" temperature
The program checks the temperature and prints a message on hot days.
if temperature > 25 then
printfn "It is a hot day"
The condition temperature > 25 is a Boolean expression. The
block after then is indented, like any other
code block. The last line starts in the
first column again, so it runs in either case.
λ dotnet fsi if_then.fsx It is a hot day Temperature: 28 °C
The else branch
An else branch runs when the condition is
false. Exactly one of the two blocks runs.
let balance = 120.0
let price = 150.0
if balance >= price then
printfn "Payment accepted"
printfn "New balance: %.2f" (balance - price)
else
printfn "Payment declined"
printfn "Missing amount: %.2f" (price - balance)
A payment is accepted only if the balance covers the price.
else
printfn "Payment declined"
printfn "Missing amount: %.2f" (price - balance)
The else keyword is aligned with if. Both
branches are blocks that can hold several lines.
λ dotnet fsi if_else.fsx Payment declined Missing amount: 30.00
Several branches with elif
The elif keyword, short for else if, adds more
conditions. They are tested from top to bottom, and the first one that
holds selects its branch.
let score = 78
if score >= 90 then
printfn "%d points: grade A" score
elif score >= 75 then
printfn "%d points: grade B" score
elif score >= 60 then
printfn "%d points: grade C" score
else
printfn "%d points: grade F" score
The program turns a test score into a grade.
elif score >= 75 then
A score of 78 fails the first condition and passes the second one. The
remaining conditions are not tested. The order is important: if the
score >= 60 test came first, every score of 60 or more
would get a C. The last else catches all scores below 60.
λ dotnet fsi elif.fsx 78 points: grade B
if returns a value
Since if is an expression, its value can be bound to a name.
The value is the value of the branch that runs. This replaces the ternary
operator ?: of C-like languages.
let age = 17
let status = if age >= 18 then "adult" else "minor"
let fee =
if age < 6 then 0
elif age < 18 then 5
else 10
printfn "%d years: %s" age status
printfn "Entrance fee: %d EUR" fee
The status and the fee are computed from the age.
let status = if age >= 18 then "adult" else "minor"
A short conditional fits on one line. The value of status is
"minor", a string.
let fee =
if age < 6 then 0
elif age < 18 then 5
else 10
A longer conditional is written on several lines, with each branch
followed by its value. The value of fee is an integer.
λ dotnet fsi if_value.fsx 17 years: minor Entrance fee: 5 EUR
Branches have the same type
An expression has one type, so all branches of an if must
return values of the same type. The compiler checks this before the
program runs.
let count = 3 let label = if count = 1 then "one item" else count printfn "%s" label
The first branch returns a string and the second one an integer.
let label = if count = 1 then "one item" else count
The type of the first branch decides the type of the whole expression. The
else branch returns an int, and the script stops
with error FS0001. Writing string count in the
else branch would fix it. The path of the file is shortened
in the output.
λ dotnet fsi branch_types.fsx branch_types.fsx(3,47): error FS0001: All branches of an 'if' expression must return values implicitly convertible to the type of the first branch, which here is 'string'. This branch returns a value of type 'int'.
A missing else branch
Without else, the expression has no value when the condition
is false. For this reason, an if without else
must have the type unit: its branch may only do something,
such as printing, and cannot return a value.
let stock = 0
if stock = 0 then
printfn "The product is sold out"
let message = if stock = 0 then "sold out"
printfn "%s" message
The first conditional is correct, the second one is not.
if stock = 0 then
printfn "The product is sold out"
The printfn function returns unit, so this
if does not need an else branch.
let message = if stock = 0 then "sold out"
This branch returns a string, but nothing is returned when the stock is
not zero. The compiler reports error FS0001 and suggests adding an
else branch. The unit type is covered in
F# unit type.
λ dotnet fsi missing_else.fsx missing_else.fsx(6,33): error FS0001: This 'if' expression is missing an 'else' branch. Because 'if' is an expression, and not a statement, add an 'else' branch which also returns a value of type 'string'.
Conditions are Booleans
A condition must have the type bool. F# does not treat
numbers, strings or empty values as true or false, as C or Python do.
let items = 3
if items > 0 then
printfn "The cart has %d items" items
if items then
printfn "The cart is not empty"
The second condition is an integer.
if items then
The compiler expects a bool and finds an int. A
condition is written as a comparison, here items > 0 or
items <> 0.
λ dotnet fsi bool_condition.fsx
bool_condition.fsx(6,4): error FS0001: This expression was expected to have type
'bool'
but here has type
'int'
Combining conditions
Conditions are combined with the Boolean operators &&
(and), || (or) and not, covered in
F# operators.
let age = 67
let isStudent = false
let hasCard = true
if age < 26 || isStudent then
printfn "Youth discount"
elif age >= 65 && hasCard then
printfn "Senior discount"
else
printfn "Full price"
if not hasCard then
printfn "Ask for a customer card"
else
printfn "Card accepted"
let discount = if age >= 65 && hasCard then 0.3 else 0.0
printfn "Discount: %.0f %%" (discount * 100.0)
A shop gives discounts to young people, students and seniors who have a customer card.
elif age >= 65 && hasCard then
The && operator requires both conditions. The
&& and || operators short-circuit: the
second condition is evaluated only when the first one does not decide the
result.
if not hasCard then
The not function negates a Boolean value. In the format
string, %% prints a percent sign.
λ dotnet fsi combined.fsx Senior discount Card accepted Discount: 30 %
Nested conditionals
A branch is a block, and a block can contain another if
expression. The example computes the number of days in a month, which
needs a leap-year test for February.
let year = 2024
let month = 2
let isLeap =
(year % 4 = 0 && year % 100 <> 0) || year % 400 = 0
let days =
if month = 2 then
if isLeap then 29 else 28
elif month = 4 || month = 6 || month = 9 || month = 11 then
30
else
31
let kind = if isLeap then "a leap year" else "a common year"
printfn "%d is %s" year kind
printfn "Month %d of %d has %d days" month year days
A year is a leap year if it is divisible by 4, unless it is divisible by 100 and not by 400. The year 2024 is a leap year; 1900 is not.
let days =
if month = 2 then
if isLeap then 29 else 28
The inner if is the value of the first branch. It is indented
under then, so it belongs to that branch only. Each branch of
both expressions returns an int.
elif month = 4 || month = 6 || month = 9 || month = 11 then
30
April, June, September and November have 30 days. The same choice can be written more clearly with a match expression and or-patterns; see F# match expression.
λ dotnet fsi days_in_month.fsx 2024 is a leap year Month 2 of 2024 has 29 days
Source
F# language reference: Conditional expressions: if...then...else
F# language reference: Boolean operators
In this article we have run code with if...then, added
else and elif branches, used if as an
expression that returns a value, met the rules for branch types and a
missing else, combined conditions and nested conditionals.
Repeating code is covered in F# for and while
loops, and choosing between many cases in
F# match expression.
Author
List all F# tutorials.