How to use if expressions in a column expressions node

Hi,
sorry that I missed this post concerning the Column Expressions node. In another thread (see here) I broke down the syntax of the Column Expressions and Variable Expressions node and how to use it.
I’ve already mentioned that it is possible to use loops (for, while) and if-else statements.

As previous posts already mentioned, you have the following syntax:

if(**state**) {
    // do this, if **state** evaluates to true
} else {
    // do that, if **state** evaluates to false
}

So this is rather a programmatical approach of a problem. The if-else statement creates a branch in the executing expression, that is executed from top to bottom. As soon as the if statement is reached, the state is being evaluated. Depending on that evaluation either the if block (enclosed by the following ‘{’ and ‘}’) is entered or the else block (with it respective braces). Thus, the if-else statement yields a branch in the usual evaluation flow, where either one of the blocks is evaluated (but not both).

While you are able to leave the else block empty, you can also completly skip it, e.g.

// previous code
if(**state**) {
    // do this if state evaluates to true
}
// do this independently of **state**

Instead of nesting if-else-blocks like this

if(**state1**) {
    // do this, if **state1** evaluates to true
} else {
    if(**state2**) {
        // do this, if **state1** evaluates to false and **state2** evaluates to true
     } else {
         // do this if neither **state1** nor **state2** evaluate to true
    }
}

You can write

if(**state1**) {
    // do this, if **state1** evaluates to true
} else if (**state2**) {
   // do this, if **state1** evaluates to false and **state2** evaluates to true
} else {
   // do this if neither **state1** nor **state2** evaluate to true
}

You can add as many else-if blocks as you like and you can always skip the last else block.

Now concerning the actual if-statements (denoted by state, state1, and state2 in the previous examples):
The Column Expressions and Variable Expressions nodes provide different functions that evaluate a specific input into a boolean value (true of false). Some useful functions, which can usually be used among all types of input are grouped under the category Logical. This group contains functions like and(val1,val2,…), which evaluates to true if all provided values (or nested functions) evaluate to true, or(val1,val2,…), which evaluates to true if at least one provided value (or nested function) evaluates to true, and equals(val1,val2), which evaluates to true, if val1 equals val2.
Depending on the category there might be more logical functions (usually denoted by the Return Type: Boolean). In addition to that, you can also compare different types (based on the column type) accordingly, as for numbers we have:

  • < (less than)
  • > (greater than)
  • <= (less or equal than)
  • >= (equal or greater than)

Note, that this, while still easy to use for numbers, might be more advanced for other non-numerical types.

The difference to the Rule Engine node is explained rather simple. While the Rule Engine node might provide its own easy-to-use syntax its main purpose is to match user specified rules to specific values. This means that the syntax looks as follows:

**state** => **value**

While this can usually be easily translated into the Column Expressions syntax:

if (**state**) {
    **value**
}

you can, instead of simply return a value (which will be assigned to the output column) do calculations depending on the evaluation and continue with your calculations.

As mentioned in the other thread the syntax is based on JavaScript and thus, the if-else-statements behave accordingly (and similarly to other programming/scripting languages).

I hope this answered helped a little bit to clarify, how to use these statements and what the difference between the _if-else_blocks in a Column Expressions node and the rules in a Rule Engine node are.
If there are any questions (especially concerning the Expression-nodes), don’t hesitate to mention me directly in your posts.

Cheers,
Moritz

16 Likes