Ternary operation in a formula
A ternary operation in programming is a concise way to perform conditional evaluations. The ternary operator takes three operands and is typically used as a shorthand for an if-else
statement.
Syntax
The general syntax of a ternary operation is:
condition ? expression_if_true ; expression_if_false
Explanation
- condition: This is the expression that is evaluated. If it evaluates to true, the first expression is executed; if false, the second expression is executed.
- expression if true: This expression is returned if the condition is true.
- expression if false: This expression is returned if the condition is false.
Example
Create a Boolean parameter “switch” with value true
Create a Point.1
with coordinates (10, 0, 0)
Create a Point.2
with coordinates (-10, 0, 0)
Create a parameter of type point and in the formula editor type:
switch == true ? Point.1 ; Point.2
You might need to select the points from the tree to be sure the complete path is caught
In this example, the condition checks if switch
is true or false. If true, it assigns “Point.1” to the point parameter, otherwise, it assigns “Point.2”
It can be used directly in formulas, making it useful for assignments. For complex conditions, a rule or reaction can still be used.