Following the ML principle,
programming is done by defining an expression that represents the desired value.
conditional computation is represented as an expression.
A conditional expression
if then else
is an expression that computes a value in the following steps.
-
1.
Evaluate and obtain a value.
-
2.
If the value is true then evaluate and return its value.
-
3.
If the value of is false then evaluate return its value.
true, false are two constants of type bool.
Expressions of type bool contains comparison expressions
and logical operations.
# 1 < 2;
val it = true : bool
# 1 < 2 andalso 1 > 2;
val it = false : bool
# 1 < 2 orelse 1 > 2;
val it = true : bool
Conditional expressions are made up with expressions of type
bool.
# if 1 < 2 then 1 else 2;
val it = 1 : int
Since this is an expression, it can be combined with other
expressions as show below.
# (if 1 < 2 then 1 else 2) * 10;
val it = 10 : int