if / then / else
Try it yourself →Produces a value based on a condition.
Syntax
if condition then value1 else value2
Description
In Haskell, if is an EXPRESSION: both then and else are mandatory and both produce a value. even/odd tells if a number is even/odd.
Example
main = do let number = 7 putStrLn (if even number then "even" else "odd")
Expected output
odd
When to use?
Branching, conditional value selection.
Common mistakes
- ⚠️ An if cannot be written without else (if is an expression).