Expressions are written by enclosing an expression body inside parentheses (). The following table lists the valid expression body values:
Expression Body |
Example |
Operand |
X. |
Unary operator followed by an operand |
–X. |
Binary operator between two operands |
X / Y. |
An operand can be any of the following items:
Operand |
Example |
Number |
100 |
String |
"Hello" |
Date |
01/01/2000 |
Constant |
C_Maximum |
Variable |
iCount |
Function call |
Length("Hello") |
Expression Body |
X + Y |
Expression |
(X + Y) |
Operators are categorized into several groups depending upon the type of expression. The table below lists all the possible operators:
Binary Arithmetic Operators |
Operation |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
^ |
Power of |
Max |
Maximum |
Min |
Minimum |
Unary Arithmetic Operator |
Operation |
- |
Sign negation |
Relational Operators |
Operation |
= |
Equal |
<> |
Not equal |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
Contains |
Member of |
Matches |
String compare with wildcards |
Boolean Operators |
Operation |
Not |
negation |
And |
logical and |
Or |
logical or |
String Operators |
Operation |
+ |
Concatenation |
- |
Trim "outer" spaces and concatenate |
* |
Concatenate with a single space between arguments |
Bitwise Operators |
Operation |
Iand |
Bitwise and |
Ior |
Bitwise or |
Here are some examples of expressions:
While ((iTime < 12) and not((sDay = "Sat") or (sDay = "Sun")))
//…
Loop
This is a Boolean expression that would return True if the variables iTime and sDay were set to a weekday before 12pm.
Move (sLastName + ', ' + sFirstName + ' ' + sInitial) To sFullName
This is a string expression that concatenates two string variables with some string constants.
string sFirst sLast sName
move " Joe " to sFirst
move " Smith " to sLast
move (sFirst - sLast) to sName
This is a string expression that concatenates two string variables using the - operator, which trims all trailing spaces from the first argument and all leading spaces from the second argument, resulting in " JoeSmith " stored in sName.
string sFirst sLast sName
move " Joe " to sFirst
move " Smith " to sLast
move (sFirst * sLast) to sName
This is a string expression that concatenates two string variables using the * operator, which trims all trailing spaces from the first argument and all leading spaces from the second argument, leaving only a single space between the two strings, resulting in " Joe Smith " stored in sName.
If an expression triggers an error, the result of that expression is undefined and therefore unreliable.
For example, the below expression will trigger an "invalid symbol in expression" error, since the string constant "Fred" cannot be converted to a number. Thus, the contents of variable nValue is undefined after the expression returns and should not be used.
Number nValue
move "Fred" to nValue
Boolean expressions are evaluated to a result of True or False. In DataFlex any non-zero integer value can represent the Boolean value of True. The integer 0 represents the Boolean value of False.
True <> 0
False = 0
Expressions are evaluated left to right. DataFlex performs short circuit evaluation of Boolean expressions. This means that if the outcome of the entire Boolean expression can be determined by only evaluating part of it then the outcome is returned without evaluating the rest of the expression. This can occur with Boolean and and or operators as demonstrated in the following examples:
If ((iValueX = 1) or (iValueY = 2)) Begin…
If ((iValueX = 1) and (iValueY = 2)) Begin…
In the first example if (iValueX = 1) is true then (iValueY = 2) is not evaluated because regardless of the outcome the entire expression will still evaluate to true. The same is true for the second example if (iValueX = 1) is False.
You most need to be aware of short circuit evaluation when your expression contains a function call as in the following example:
If ((iValueX = 1) or (GetNextCharacter() = 'X')) Begin…
In this example the function GetNextCharacter is never executed if (iValueX = 1) is True. If it is important that the GetNextCharacter function is executed, then you should re-write the expression in the following way:
Move (GetNextCharacter()) To sCharacter
If ((iValueX = 1) or (sCharacter = 'X')) Begin…
Special Note: DataFlex expressions do not follow any rules of precedence among operators. You must use parentheses to force precedence. For example:
(1 + 2 * 4)
Should be written:
(1 + (2 * 4))
If you want the 2 * 4 part to be evaluated before adding one. DataFlex would evaluate the first example as 12 and the second as 9.