if conditional statements are used to execute code blocks based on a conditional expression. F# provides the following conditional statements
It provides the following features
- simple if statements
- if else statements
- if else if else
- nested if else
F# conditional statements are expressions that return boolean type - true and false.
F# uses spaces to indent inside code blocks to have scopes instead of {}
.
F# if conditional statement
F# if the conditional statement is used to execute code block based on conditional expression statements.
The conditional expression contains either single or multiple conditions.
if the conditional expression is always evaluated as true
or false
. if it is true
, It executes a code block.
Syntax
if condition
//code block
Here is an example
if condition {
//code block
}
condition is a Boolean expression that returns true or false
- if the condition is true, the code block is executed.
- if the condition is false, the code block is not executed. Here is an example of f# if conditional true example
let number: int32=20;
if number%2=0 then
printfn "%d is an Even Number " number
Output:
20 is an Even Number
Here is a simple if conditional statement false example
let number: int32=21;
if number%2<>0 then
printfn "%d is an Odd Number " number
Output:
21 is an Odd Number
F# if else conditional statements
if else in f# is used to execute code blocks based on true and false conditional expressions.
Syntax:
if condition then
//code block based on true conditional value
else
// code block based on false conditional value
condition is a Boolean expression that returns true or false
- if the condition is true, code block inside if block is executed.
- if the condition is false, the code block inside the else block is executed. Here is an example of f# if conditional true example
Here is a java if else conditional expression example
let number: int32=25;
if number%2=0 then
printfn "%d is an Even Number " number
else
printfn "%d is an Odd Number " number
Output:
25 is an Odd Number
F# if then elif else statement example
This is a combination of if and elif and if statements.
This is used to execute code blocks based on multiple conditions.
Syntax:
if condition1 then
//code block executes based on the condition1 is true
elif condition2 then
//code block executes based on the condition2 is true
else
//code block executes based on the condition1 is false
Here is an example
let number: int32=21;
if number < 9 then
printfn "%d is a Single Digit Number " number
elif (number > 9) && (number<99) then
printfn "%d is a Two-Digit Number " number
elif (number > 99) && (number<999) then
printfn "%d is a Three-Digit Number " number
else
printfn "%d is a Four-Digit Number " number
Output:
2 is a Two-Digit Number
Nested If F# examples
if statements are nested inside another if or else or elif statements as given below
let number: int32=2;
if number < 9 then
printfn "%d is a Single Digit Number " number
if number%2=0 then
printfn "%d is an Event Number " number
else
printfn "%d is an Odd Number " number
elif (number > 9) && (number<99) then
printfn "%d is a Two-Digit Number " number
elif (number > 99) && (number<999) then
printfn "%d is a Three-Digit Number " number
else
printfn "%d is a Four Digit Number " number
Output
2 is a Single Digit Number
2 is an Event Number