if expression specifies that some code should only be evaluated if a certain condition is true. For example:
The condition must be an expression of type
if expression can optionally include an
else clause to specify another expression to evaluate when the condition is false.
Either the "true" branch or the "false" branch will be evaluated, but not both. Either branch can be a single expression or an expression block.
The conditional expressions may produce values so that the
if expression has a result.
The expressions in the true and false branches must have compatible types. For example:
else clause is not specified, the false branch defaults to the unit value. The following are equivalent:
if expressions are used in conjunction with expression blocks.
if-expression → ( expression ) expression else-clauseopt else-clause → else expression