Haskell - Nested if-else statement


In the above example, we have seen the use of if-else statement in Haskell. Here, we will learn how to use multiple if-else statements in one Haskell program.

In Haskell, multiple lines of if will be used by separating each of the if statement with its corresponding else statement.

The following code shows how you can use nested if-else statement in Haskell −

main = do   
   let var = 26 
   if var == 0 
      then putStrLn "Number is zero" 
   else if var `rem` 2 == 0 
      then putStrLn "Number is Even" 
   else putStrLn "Number is Odd"

In the above example, we have introduced multiple conditions in one function. Depending on the function inputs, it will provide us different outputs. You can change the value of the variable "var" to check all the conditions.

Our code will produce the following output −

Number is Even