Conditional Operations
Ternary If Statements
Conditional operations allow users to check for certain conditions and then assign different values depending on the outcome. This is commonly implemented as if-then-else statements in programming. Another common form, but with fewer characters/keystrokes, is the inline if, or ternary if.
The ternary operator is a conditional expression that includes three operands:
- a condition that evaluates to true or false
- outcome 1 (if true)
- outcome 2 (if false)
The syntax for a ternary if statement with only one condition to evaluate follows:
condition
|
?
|
value if condition is true
|
:
|
value if condition is false
|
The question mark (?) separates the condition from the values, and the colon (:) is used to separate the values to be used after evaluating the condition.
For example, the expression x ? y : z evaluates to y if the value of x is true. If x is false, the operation evaluates to z.
Nesting Multiple Ternary If Statements
Ternary if statements can also be nested to evaluate more than one condition. Here is the syntax when evaluating two conditions:
condition1
|
?
|
condition2
|
?
|
value1
|
: | value2
|
: | value3
|
As shown below, adding parentheses around the nested condition and value pair makes the query more readable, but they do not alter the results.
condition1
|
?
|
(condition2
|
?
|
value1
|
: | value2)
|
: | value3
|
The table below shows how the multiple conditions are evaluated logically:
|
Condition1 |
Condition2 |
Value |
|---|---|---|
|
True |
True |
value1 |
|
True |
False |
value2 |
|
False |
[skipped] |
value3 |
Let's look at the following example of a nested ternary if operation:
x ? (y ? 1 : 2) : 3
- If both
xandyare true, the expression evaluates to 1. - If
xis true, andyis false, the expression evaluates to 2. - If
xis false, the expression evaluates to 3.
Example
The example below illustrates how to use a conditional operation (ternary if) with the update() method in a Deephaven query.
The table below has two columns of data. We want to check each existing row for null values. We then want to create a new column in the table and insert different values in that new column based on whether null values are present in either or both columns for that given row.
t
|
ColA |
ColB |
|---|---|
|
|
|
|
|
20 |
|
30 |
|
|
40 |
55 |
The following query can be used to process that logic:
t=t.update("ColC = isNull(ColA) ? (isNull(ColB) ? 0 : ColB) : ColA")
The query begins with the .update method, which defines a new column, ColC.
The value to be used for ColC will be determined by the conditions contained in the embedded ternary if statement, highlighted below:
t=t.update("ColC = isNull(ColA) ? (isNull(ColB) ? 0 : ColB) : ColA")
The possible outcomes of each condition are represented in the table below:
|
Condition 1 |
Condition 2 |
Value to insert |
|---|---|---|
|
True |
True |
0 |
|
True |
False |
ColB |
|
False |
[skipped] |
ColA |
Here's the table generated after running the query:
t
|
ColA |
ColB |
ColC |
Explanation |
|---|---|---|---|
|
|
|
0 |
The values in ColA and ColB are both null, so ColC is set to 0. |
|
|
20 |
20 |
The value in ColA is null, but the value in ColB is not null, so, is set to the same value as that in ColB. |
|
30 |
|
30 |
The value in ColA is not null, so ColC will be set to the same value as ColA. |
|
40 |
55 |
30 |
The value in ColA is not null, so ColC will be set to the same value as ColA. The value present in ColB does not matter.
|
Last Updated: 16 February 2021 18:07 -04:00 UTC Deephaven v.1.20200928 (See other versions)
Deephaven Documentation Copyright 2016-2020 Deephaven Data Labs, LLC All Rights Reserved