In this article
The Expression Builder helps you to build JScript expressions. To use the Expression Builder:
- Activate a condition object either by double clicking on its IF branch, or by right-clicking it and selecting Edit from the menu.
- Single-click on the question in the Survey Tree on which you want to base your expression (double-clicking will activate form-edit mode).
The Expression Details form opens - .
Figure 1 - Example of the Expression Builder
The expression must be based on a question, so:
The Expression Builder for the chosen question opens - .
Figure 2 - The Expression Builder form for the multi question
Note: If you select a question type that is not supported in the Expression Builder, for example a Date object, then the message "Enter expression in the area above" is displayed. You can then type your expression into the field "manually".
You can now build the expression in the Preview area. Once the expression (or part of the expression) is as required in the Preview area, click Add to expression to add it to the Expression field. Note that the Preview expression will be added to the Expression field at the cursor position.
The Expression Builder consists of four columns:
- Operators - This lists the operators you can apply to the question object in focus. Only those operators that are valid for the current cursor position in the expression will be available. The list will be different for each type of question because the various question types support different methods. Click on an item in the list to insert the operator into the preview.
- Variables - This shows the question ID of the selected question. If the question is a grid, each of the answers in the grid (which are themselves single questions) will be shown. Click on an item in the list to insert a reference to the question in the preview.
- Functions - This is a list of the functions you can apply to the question in focus. The list will be different for each type of question because the various question types support different methods. Click on an item in the list to insert the function into the preview. Note that only functions that are applicable to the current cursor position will be available; all others will be grayed out. Function inputs are restricted to logical values; for example, a Length value must logically be numerical, so only numerical values can be input. Hover the cursor over a function to show a tool-tip describing the function.
- Value - This list shows the answers for the selected question, with the code in square brackets. Click on an item in the list to insert the code into the preview.
These lists allow you to build complex expressions without having to remember the JScript-syntax. You can at any time single-click another question in the Survey Tree to expand your expression.
Note: The symbols listed in the Functions column and the symbols used in the expression may differ. Those shown in the Functions column are generally recognized as being the "international standards" for the relevant functions, whereas the symbols used in the Preview and Expression fields are those used in JScript.
The single/grid operators are as shown in the table below:
Description | Symbol |
Logical NOT | ! |
Less than | < |
Greater than | > |
Less than or equal to | <= |
Greater than or equal to | >= |
Equality | == |
Inequality | != |
Logical AND | && |
Logical OR | || |
The multi operators/methods are as shown in the table below:
Description | Method/operator |
Includes | .inc( |
Union | .union( |
Intersection | .isect( |
Difference | .diff( |
Logical AND | && |
Logical OR | || |
Logical NOT | ! |
End parenthesis | ) |
Comma | , |
To build effective conditions you must be aware of operator precedence. When Authoring evaluates conditions, the operations are performed in a controlled order. Operations with a higher precedence are performed before those with a lower one. The table below shows this order from highest to lowest.
Precedence | Operator |
7 | () |
6 | ! |
5 | <, <=, >, >= |
4 | ==, != |
3 | && |
2 | || |
1 | = |
Use parentheses to change the order of evaluation of operations. Expressions in parentheses are fully evaluated before the value is used in the rest of the expression. Each part in a condition must be a complete statement. For example: Assume you have an age question with five answer alternatives and you want to include a condition that selects only respondents who have answered ‘1’ or ‘2’.
The correct condition is:
IF f(‘q3’) == ‘1’ || f(‘q3’) ==‘2’ THEN…The condition below does not use the correct syntax:
IF f(‘q3’) == ‘1’ || ‘2’ THEN…Note: You can see from the expressions above that the codes 1 and 2 have apostrophes before and after. This is because the "codes", i.e. the answers to questions, are always stored as strings in the database. You must therefore be careful when using the greater than (>) or less than (<) operators in expressions.
Consider how you sort items alphabetically (as in a dictionary). When treated as strings, a sequence of numbers 1,2,3,…,10,11 will be sorted as: 1,10,11,2,3,…,9. So f('q1')<'3' will be TRUE for the numbers 1,10,11 and 2. If you are constructing an expression with greater than or less than you should convert the codes to numbers. Use a method called .toNumber() to convert.
Example:
f("q1").toNumber()<3Using this, the expression will be true for 1 and 2, as expected.
Always test your survey to see whether the condition works as intended.