F# if then
Syntax
The syntax for if then statement in F# is as follows:
if condition then
// code to execute when the condition is true
else
// code to execute when the condition is false
Example
Here is an example of if then statement in F#:
let number = 10
if number % 2 = 0 then
printfn "%d is an even number." number
else
printfn "%d is an odd number." number
Output:
10 is an even number.
Explanation
if then
statement is used in F# to execute a specific code block if a condition is true and another code block if the condition is false. In the above example, the condition is checked using %
operator to see if the remainder of number
divided by 2
is equal to 0
. If it is true, it prints number
is an even number, otherwise, it prints number
is an odd number.
Use
if then
statement is used for decision making based on the result of a condition. It is commonly used in programming to execute specific code based on the result of a condition.
Important Points
- In F#, the
if then
statement must have onethen
block and optionally oneelse
block. - The
condition
must be a boolean value or an expression that evaluates to a boolean value. - The
then
block andelse
block must be indented one level deeper than theif
statement.
Summary
if then
statement is used for decision making based on the result of a condition. It executes a specific code block if a condition is true and another code block if the condition is false. The condition
must be a boolean value or an expression that evaluates to a boolean value. The syntax is if condition then expression1 else expression2
.