f-sharp
  1. f-sharp-if-then-elif-ladder

F# if then elif ladder

Syntax

if condition1 then
   // code to execute if condition1 is true
elif condition2 then
   // code to execute if condition2 is true
elif condition3 then
   // code to execute if condition3 is true
else
   // code to execute if none of the above conditions are true

Example

let age = 25

if age < 18 then
    printfn "You are a minor"
elif age >= 18 && age < 60 then
    printfn "You are an adult"
else
    printfn "You are a senior citizen"

Output

You are an adult

Explanation

In F# programming, if-elif-else ladder is used to execute one of many possible conditions. The if keyword is used to check the first condition, and the elif keyword is used to check further conditions. If none of the conditions are true, the else block is executed.

In the above example, the age of a person is checked using the if-elif-else ladder. If the age is less than 18, it prints "You are a minor". If the age is between 18 and 60, it prints "You are an adult". Otherwise, it prints "You are a senior citizen".

Use

The if-elif-else ladder is used in F# programming to execute one of the multiple possible conditions.

Important Points

  • The elif keyword is used to check multiple conditions in an if-elif-else ladder.
  • The else block is executed if none of the conditions are true.
  • The if-elif-else ladder can have any number of elif conditions.

Summary

In F# programming, the if-elif-else ladder is used to execute one of the multiple possible conditions. It is useful when there are multiple conditions and only one of them needs to be executed based on the input.

Published on: