Using conditional statements
@If lets you execute one statement or another, depending on whether a condition is True or False. A condition is typically the comparison of values, but can also be a constant, a variable, or the result of an @function. For example:
- @ViewTitle = "By Author" is True if the name of the current view is "By Author."
- @Elements(Categories) > 0 is True if Categories has at least one element.
- 1 used as a condition means True. @True and @Yes return 1. Comparisons and @functions that evaluate to a condition return 1 if the condition is True.
- 0 used as a condition means False. @False and @No return 0. Comparisons and @functions that evaluate to a condition return 0 if the condition is False.
The @If statement has an odd number of parameters with a minimum of three, as follows:
- The condition is the first parameter and every other parameter thereafter if the @If statement has multiple conditions.
- The statement that is executed if the condition is True is the second parameter and every other parameter thereafter if the @If statement has multiple conditions.
- The statement that is executed if the condition is False is the last parameter.
The simplest @If statement has the following syntax:
@If(condition; True statement; False statement)
An @If statement with three conditions has the following syntax:
@If(condition1; True1; condition2; True2; condition3; True3; False)
The @If function is evaluated left to right, and the first condition that is True causes its corresponding True statement (that is, the next parameter) to be processed. No further evaluation or processing within the @If statement takes place. If none of the conditions are True, the False statement (that is, the last parameter) is processed.
The True and False statements take various forms, depending on their context:
- If the @If statement is the last statement in a formula that evaluates to a result, the True and False statements must evaluate to a result.
- If the @If statement is the righthand side of an assignment, the True and False statements must evaluate to a value suitable to assignment to the field or temporary variable on the left side.
- Otherwise, the True and False statements must cause an action.
Assignments for True and False statements take two forms:
- The "classic" form assigns the result of the entire @If statement
where each True and False statement consists only of a value:
variable := @If(condition; value1; value2)
- Alternatively you can make each True and False statement a complete
assignment:
@If(condition; variable := value1; variable := value2)
Note: The second form is new with Release 6.
The parameters of @If can themselves be @If statements. Nested @If statements are useful to work around the limited logic constructs in the formula language, but make for complicated syntax.
The @Do function provides a means to execute multiple statements on a True or False condition.