Special Expression Formats
Expressions can be combined in special ways to produce concise program statements that nevertheless have tremendous power and flexibility. The special expression formats are
- WHERE expression format
- assignment expression format
- grouped expression format
- case expression format
How To Use the WHERE Expression Format
A WHERE subcommand can be appended to a value expression to control the evaluation of that value expression. The syntax is
valexpression WHERE logic_expression
where valexpression is any value expression.
logic_expression is any logic expression.
If the expression is complex, it must be enclosed in parentheses.
If the WHERE condition evaluates to $True, the value expression is evaluated. If the WHERE condition evaluates to $False, the value expression is not evaluated, and the entire expression is considered to be $Null.
The WHERE expression format is frequently used with the case expression format, the assignment expression format, or with aggregate functions.
Examples of the WHERE Expression Format
(Salary – 20000) where Salary > 20000
(Salary – 20000) is evaluated only if the condition is met. The entire expression format takes on the value of the arithmetic expression if the condition is met; otherwise, it is considered to be $Null (valueless). The following example
let i = { 1 where Age < 10,
2 where Age between 10 and 30,
4 where Age > 50, 3}
assigns a value to a variable based on Age. The following example
compute Employees evaluate (let TotSal =
$total(Salary where LastName = “Smith”))
totals the salaries of all employees named Smith.
How To Use the Assignment Expression Format
A LET command can be used within an expression to assign a value to a target as the target is being used in a value or logic expression. The syntax is
(LET target=expression)
Where target is a global or local variable, a parameter, a field or a form field
where expression is any value expression.
Assignment expressions enable you to use fewer commands to achieve a particular result or to perform calculations within set-processing commands.
Examples of the Assignment Expression Format
while (let vCount=vCount-1)>0
Tests that vCount (which is first set to vCount – 1) is greater than 0.
let vTriple=(let vSingle=5)*3
Sets vSingle to 5 and vTriple to vSingle * 3.
(let vCount=vCount+1) where Processed=”yes”
Increments VCount if Processed is “yes”.
report footing (let TotalSal=$total(Salary)*1000)
Sets the variable TotalSal to 1000 times the running total of Salary, displaying TotalSal in the report footing. After the latter command has been executed (i.e., when the report is finished), the application retains access to the contents of TotalSal.
How To Use the Grouped Expression Format
Individual value expressions may be placed in parenthesis and separated from one another by commas to produce “grouped expressions”. The syntax is
(expression «,expr»)
where expression is any value expression. If the expression is complex, it must be enclosed in parentheses.
All the expressions are evaluated. The entire expression takes on the value of the last expression in the group.
Grouped expressions are useful for performing multiple assignments within one logic expression.
Example of the Grouped Expression Format
while ((let vTotal=0), (let vAverage=0), (let vCount=vCount-1)) > 0
let var1=((let var2=2), (let var3=3), 5-4)
How To Use the Case Expression Format
Case expressions provide a powerful method of dealing with evaluations that are conditional on the results obtained. The syntax is
{expression«, expr»}
where expression is any value expression, including another case expression.
The expressions within the braces are evaluated from left to right. The entire expression takes on the value of the first expression that is not $Null (valueless).
Each case within the braces is evaluated from left to right until one case yields a value that is not $Null. The case expression returns this value.
Case expressions can minimize the code that is required to produce certain conditional results as shown in the following example:
let Status = {“tall” where Height > 6, “short”}
replaces
if Height > 6
let Status = “tall”
else
let Status = “short”
endif
Case expressions can ensure that a value is provided in situations where a field or form field alone may sometimes be $Null, as shown in the following example:
let Salary = {fAddEmps.Salary, 0}
Case expressions can also be used to determine the action to be taken by certain commands, as shown in the following example:
break 1
{$year(InvDate) + 1 where $month(InvDate) >= 5,
$year(InvDate)}
heading …
Examples of the Case Expression Format
let Status={“tall” where Height>6, “short”}
let ESalary={fAddEmps.Salary, 0}
detail line “Employee Number: ” {EmpNum, “N/A”}
See Also