F# - Nested if Statement



It is always legal in F# programming to nest if/then or if/then/else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax

if expr then
   expr
   if expr then
      expr
   else
      expr
else
   expr

Example

let a : int32 = 100
let b : int32 = 200

(* check the boolean condition using if statement *)

if (a = 100) then
(* if condition is true then check the following *)

   if (b = 200) then
      printfn "Value of a is 100 and b is 200\n"
printfn "Exact value of a is: %d" a
printfn "Exact value of b is: %d" b

When you compile and execute the program, it yields the following output −

Value of a is 100 and b is 200

Exact value of a is: 100
Exact value of b is: 200
fsharp_decision_making.htm
Advertisements