If
Checks a specified expression and returns one of two values depending on whether it evaluates to True or False.
The If function can also be used to create compound statements.
Syntax
If( Expression, FirstValue, SecondValue );
Parameters
Parameter Name | Type | Description | Optional? | Default Value |
---|---|---|---|---|
Expression | String | The expression to check. | No | n/a |
FirstValue | String | The value to return if the expression evaluates to True. | No | n/a |
SecondValue | String | The value to return if the expression evaluates to False. | No | n/a |
Output
If the specified expression evaluates to True, the function will return the FirstValue; otherwise, it will return the SecondValue.
Use Case
If is one of the simplest KnowledgeKube functions, but is nonetheless integral to constructing conditional logic. Any situation where you want to create multiple possible outcomes in your expression, you can use an If statement. Doing this in programming is known as Branching your code.
For example, say you want to assign a group of football players into two teams based on a number they pick out of a hat. The numbers range from 1-22, with all the people with numbers from 1 to 11 being assigned to the blue team, and numbers 12 and up being assigned to the red team.
AssignedTeam = If( PickedNumber <= 11, "Blue", "Red" );
As you can see, the expression here is “PickedNumber <= 11”, meaning that if the value of PickedNumber is less than or equal to 11, the expression equates to True and the value of AssignedTeam will be the first value (“Blue”). Otherwise, the value of AssignedTeam is that of the second value (“Red”).