# Conditional Expressions: if...then...else

The if...then...else expression runs different branches of code and also evaluates to a different value depending on the Boolean expression given.

## Syntax

if boolean-expression then expression1 [ else expression2 ]


## Remarks

In the previous syntax, expression1 runs when the Boolean expression evaluates to true; otherwise, expression2 runs.

Like other languages, the if...then...else construct can be used to conditionally execute code. In F#, if...then...else is an expression and produces a value by the branch that executes. The types of the expressions in each branch must match.

If there is no explicit else branch, the overall type is unit, and the type of the then branch must also be unit.

When chaining if...then...else expressions together, you can use the keyword elif instead of else if; they are equivalent.

## Example

The following example illustrates how to use the if...then...else expression.

let test x y =
if x = y then "equals"
elif x < y then "is less than"
else "is greater than"

printfn "%d %s %d." 10 (test 10 20) 20

printfn "What is your name? "

printfn "What is your age? "

10 is less than 20