Conditional Expressions
Conditional expressions are available for selection in the Expression Editor. When you click on a conditional expression, a preview is shown in the Help pane. When you double-click on a conditional expression, the expression is pasted into the editor:
If-then-else
The expression parser has a conditional operator 'if'. It has the following syntax:
if <predicate> then <expression_true> elif | elsif | elseif <predicate> then <expression_true> else <expression_false> endif | fi
The elif | elsif | elseif part of the operator can be repeated, or be skipped completely. The logic is to evaluate predicates until the one that evaluates to TRUE is found, and then evaluate the corresponding <expression_true>. If no predicate evaluates to TRUE, then <expression_false> is evaluated and this becomes the result.
You can use the 'if' operator in the following way:
MCAF + if RECOVERY > 0.5 then MCAF * 2 else 0 fi
In other words, if the RECOVERY is greater than 0.5, then add MCAF * 2 to MCAF, otherwise add 0 (nothing).
Type Conversion
Take care when using the “#” operator to convert the result of a complex expression. The result can be unexpected, especially when the “#” operator is used to convert the result type of an If-then-else expression. Let’s take a look at this in more detail.
There are 3 parts to an If-then-else statement:
- The comparison that returns either TRUE or FALSE.
- The “then” clause (applied when TRUE).
- The “else” clause (applied when FALSE).
When an If-then-else expression contains no hashes, the “then” and “else” clauses are evaluated as text or numeric depending on the precedence rules as summarised in the table below:
=3 + 4 | produces the number 7, whereas |
=#(3 + 4) | produces the text “7”, and |
=#3 + 4 | produces the text “34”, which is simply the concatenation of 3 and 4. |
Inserting the hash character before the “if”, does NOT change the way the “then” and “else” clauses are evaluated, but it does convert the final result to character.
Generally, if you want text output (labels) then you should cast the individual “then” and “else” clauses to character, by beginning each clause with the hash sign. So rather than:
=#if (...) then (...) else (...) endif
You would more likely want:
=if (...) then #(...) else #(...) endif
Here are some examples to illustrate how each variant works. The first screenshot shows the parameters used to display four points in Vizex:
Now let’s create an expression that will display only the larger value. The intention is to create a label with the suffix “Left” or “Right”, depending on whether the left or right value is larger. So, for the top left point, we want the label to be “0.73 Right”.
To get the desired result, we need this expression:
Now let’s look at some variants, and see what happens. Firstly, let’s remove the ROUND function:
Because the output is text, the Decimal column for the label is now disabled. When numbers are converted to text, the conversion is based on (the first) 7 significant digits.
What happens if we don’t use any hashes:
Now the “then” and “else” clauses are numeric (because the first value is numeric). This means that “ Left” and “ Right” are evaluated as 0. And, because the output is numeric, the Decimals column comes back into play (but the text suffix is, of course, lost).
Now if we use “#if” rather than “if”:
The “then” and “else” clauses are evaluated in the same way as the previous example (“ Left” and “ Right” are 0). But the leading hash (#if) has converted the output into a character type. This means that the Decimal column is no longer enabled, and we are back to the 7 significant digits that we get when converting a number to text.