Choosing statements to be executed according to the value of a field¶
switch |
Choose statements to be executed according to the value of a field |
SYNTAX
The switch command selects a single set of statements to be executed according to the current value of the field represented by field_id.
The concatenation of field_id and condition must in each case form a valid expression which yields TRUE (non-zero) or FALSE (zero).
The conditions are evaluated in the order they appear. As soon as a condition is found which evaluates to TRUE, then the statements for that case are executed. No further cases are tested.
If no case is true and there is no default case, then no statements within the switch construct are executed.
field_id |
The field whose value is to be tested. It may be a field from a Sculptor keyed file, or a temporary field defined in the program by !temp or !record. |
||||
condition |
The condition may include all supported arithmetical, relational and logical operators. These are listed under Table of operators, together with their order of precedence. Parentheses may be used to force a different order of evaluation. If condition is an expression without a conditional test (e.g. 100; 34 + 6; “ABC”; tmp_Field), it is evaluated in the following way:
|
||||
statements |
A block of Sculptor code. Normally, the statements for each case should terminate with a break command. This command causes statement execution to continue with the statement following the closing brace. If there is no break command, execution continues with the first statement of the next case, regardless of the condition attached to that case, and continues in this manner until a break is encountered or the closing brace reached. |
||||
default |
The statements for the optional default case are executed only if no other case has evaluated as true. However, it is still possible to fall through to the default case if the preceding case has not been terminated with a break command. The default case may be positioned anywhere in the list of cases. |
NOTES
The special label BREAK may be used on the trap clause of a Sculptor command within the statements of a switch construct. It performs exactly the same function as the break command.
The compiler permits field_id to be an expression. However, this is not a recommended practice, since it is inefficient, and may also cause unwanted side-effects since the expression is re-evaluated for each case that is tested. For example, if the expression is a function call, the function will often be called more than once.
The problem can be avoided by assigning the expression to a temporary field, for example:
tmp.Result = CalcFunc() switch (tmp.Result) { ...
Programmers must not rely on side-effects caused when switch is used with an expression. The compiler reserves the right to emit different code that would have no side effect or a different side effect.
EXAMPLE
!function ValWidth(Line, Data) {
!temp SizeNum,,i2
SizeNum = Data
switch (SizeNum) {
case < 1:
FieldProps.Width[Line] = FieldProps.Size[Line]
display FieldPropsTable
caption "Width must be greater than 0" in wintask
return ERROR
case > FieldProps.Size[Line]:
FieldProps.Width[Line] = FieldProps.Size[Line]
display FieldPropsTable
caption "Width cannot be greater than field width" in wintask
return ERROR
default:
return OKAY
}
}
RELATED TOPICS |