If Expression

If Expression  (Advanced)

Top  Previous  Next

Sometimes, rather than calculate a value, we simply want to choose between two values. This can be done using an "if" expression. Do not confuse "if" expressions with the much more common "if" statements that are described later.

 

The general form of an "if" expression is:

 

        if BOOLEAN EXPRESSION then EXPRESSION else EXPRESSION

 

For example:

 

        if Guess > Number then 75 else 20+5

 

The "if" expression evaluates to either 75 or 25 depending on the outcome of the comparison. If the comparison is true, that is, if Guess is greater than Number then the entire expression is 75; otherwise it is 25.

 

Like all expressions, an "if" expression can be used anywhere a value is used. For instance:

 

        Text(0, if Guess = Number then "Correct!" else "Incorrect")