About Boolean Expressions

Boolean expressions use Boolean operators to compare operands (typically, conditional expressions). When evaluated, Boolean expressions yield a logical result (true or false), depending on the nature of the Boolean operator and the values of the conditional expressions.

The standard rules of precedence for Boolean evaluation are used.

The software ceases to evaluate a Boolean expression as soon as the final result can be correctly predicted. For example, with OR, if the first conditional expression is logically true, then the Boolean expression must be logically true. The software does not bother to evaluate the remaining conditional expressions.

In the expression

LastName = 'Smith' or DeptNum = 'Sports'

if LastName = ‘Smith’ is logically true, then DeptNum = ‘Sports’ is not evaluated.

Note: Taking Boolean evaluation logic into account can be important to the functioning of an application program. For example, if any of the value expressions in the Boolean expression is written in the special assignment format, the assignment is not made if the portion of the Boolean expression containing the assignment is not evaluated.

Boolean Operators

OperatorMeaning
not exprNOTs the logic expression to the right. If the logic expression is true, the result of the Boolean expression is false; if the logic expression is false, the result of the Boolean expression is true.
expr and exprANDs two logic expressions (one to the left and one to the right). If both logic expressions are true, the result of the Boolean expression is true; otherwise, the Boolean expression is false.
expr or exprORs two logic expressions (one to the left and one to the right). If either (or both) of the logic expressions is (are) true, the result of the Boolean expression is true; if both of the logic expressions are false, the Boolean expression is false.
expr xor exprXORs two logic expressions (one to the left and one to the right). If either of the logic expressions is true, the result of the Boolean expression is true; if both of the logic expressions are true or if both are false, the Boolean expression is false.

The Null String

A null string is a character string of zero length. Unlike the $Null property that indicates that the value is unknown or unassigned, the null string is a known value.

For example, if you have two variables, x and y, both of which are null strings, then the expression

x = ” and y <> ‘abc’

is logically true.

Metacharacters in Character Literals

Certain characters, such as quotation marks and the backslash, have special functions in the software. If you need to use one of these metacharacters as a literal character in a string, you must “escape” the special function by inserting a backslash ( – Escape) preceding the metacharacter as shown in the following example:

‘This is a quotation mark (‘).’
‘This is a backslash (\).’

Any character string containing a metacharacter used as a literal must be enclosed in quotation marks.

Hex Codes in Character Literals

If you need a place a character not normally available from the keyboard into a character literal, use the backslash to create the hex code for that character as shown in the following example:

‘This string has an ASCII escape generated by hex 1B (1B)’

The two characters immediately following a quoted backslash are treated as hex code provided they are either digits from 0 to 9 or letters from A (a) to F (f).

Any character string containing a hex code must be enclosed in quotation marks.

Note: The escape function of the backslash inside quoted character strings can be switched off with a SET ESCAPECHAR command.

Example

Character literals that include hex codes can be useful for sending control codes to printers and terminals. For example, if the ASCII codes SI (hex 0F) and DC2 (hex 12) are used, respectively, to select and cancel condensed printing on your particular printer, use the following sequence of statements to print a report in the condensed mode:

set output printer   % switch serial output to printer
output ‘F’;        % switch condensed print on
…. commands to generate report ….
output ’12’         % switch condensed print off
set output terminal  % switch output back to terminal

About Character Literals

A character literal is any string of characters enclosed in quotation marks as shown in the following example:

'This is a character string.'
"This is a character string, too."

For convenience, you can enter character literals without the delimiting quotation marks; however, quotation marks are required in some situations. The rules for using quotation marks are described in Quotation Marks.

Valid and Invalid Literals

The following are examples of valid literals:

‘abc’Is a character string.
‘ab”C’Is a character string.
Is a null string.
3.14159Is a number literal.
9Is a number literal.
‘10.99’Is a character string.
‘John’s toy boat’Is a character string.
‘ABCd_EfG’Is a character string.
‘1234A’Is a character string.

The following are examples of invalid literals:

1 23Spaces are not permitted in number literals.
$10.99Periods are not permitted in an unquoted character string, and the dollar sign is not permitted in a number literal.
‘John’s toy boat’‘John’ is a valid character string, but the rest of the line is invalid.

About Conditional Expressions

Conditional expressions use the conditional operators to compare operands (typically, value expressions). When evaluated, conditional expressions yield a logical result (true or false), depending if  the condition is satisfied.

Conditional Operators

Operator

Condition Being Evaluated

expr = expr

The values are equal.

expr <> expr

The values are not equal.

expr < expr

The left-hand value is less than the right-hand value.

expr <= expr

The left-hand value is less than or equal to the right-hand value.

expr > expr

The left-hand value is greater than the right-hand value.

expr >= expr

The left-hand value is greater than or equal to the right-hand value.

expr between expr and expr

The value before BETWEEN is greater than or equal to the value to the right of BETWEEN and less than or equal to the value to the right of AND.

expr not between expr and expr

The value before NOT BETWEEN is less than the value to the right of NOT BETWEEN and greater than the value to the right of AND.

expr in («expr»)

The value before IN is a member of the list of values to the right of IN.

expr not in («expr»)

The value before NOT IN is not a member of the list of values to the right of NOT IN.

expr is [$]null

The value on the left is $Null.

expr is not [$]null

The value on the left is not $Null.

expr like pattern

The value before LIKE matches the pattern specified to the right of LIKE.

expr not like pattern

The value before NOT LIKE matches the pattern specified to the right of NOT LIKE.

Note: The AND and BETWEEN expressions are not the Boolean AND.

Examples of Conditional Expressions

EmpNum > 1254

Logically true when EmpNum is 1254.

DepNum >= EmpNum

Logically true when DepNum is greater than or equal to EmpNum

FirstName = ‘Smith’

Logically true when FirstName is Smith.

DeptDesc <> ‘Sports’

Logically true when DeptDesc is anything but Sports.

EmpNum between 1000 and 2000

Logically true when EmpNum is in the range 1000-2000.

LastName like ‘% ith$’

Logically true when LastName matches the given pattern.

Event.EventName in (‘F1′,’F2′,’Escape’)

Logically true when EventName is in the list.

ProdCode not between 542 and 863

Logically true when ProdCode is outside the range 542-863.

LastName not in (‘Smith’,’Jones’)

Logically true when LastName is not in the list.

FirstName not like ‘ Sm%’

Logically true when Firstname fails to match the pattern.

Simple conditional expression can be combined into more complex Boolean expressions by using Boolean operators. Also, the Boolean NOT can be used to achieve the same result as the NOT version of BETWEEN, LIKE, and IN.

 

See Also

About Boolean Expressions

Pattern Matching

The conditional operators LIKE and = (equals) can be used to construct expressions in which the condition involves matching a value to a pattern rather than to another explicit value.

The patterns are usually quoted character strings constructed using special wildcard characters in addition to literal characters.

LIKE Patterns

Patterns for use with the LIKE operator can use the following wildcard characters:

  • the underscore (_) matches any single character

  • the percent sign (%) matches zero or any number of characters

For example, the pattern

‘_ ob%’

matches any character string whose second and third letters are ob, followed by zero or more characters. Robert, Bob, and Robin (among others) would match this pattern. Therefore, the expression

FirstName like ‘_ ob%’

is logically true if the value of FirstName is Robert, Bob, Robin, or any other string that matches the given pattern.

If you need to use a percent sign or underscore as a literal character in the pattern, you must “escape” the wildcard character by inserting a backslash ( ) preceding the wildcard.  You must also escape the backslash (two backslashes in total) as shown in the following example:

Grad like ‘100\%’

Very complex expressions can be used as patterns in a LIKE comparison. For example, if SuffixVar is a variable whose value is %, then the expression

FirstName like $ concat(‘S_e%p, SuffixVar)

is logically true if FirstName matches the pattern S_E%p%.

= (Equals) Patterns

Patterns for use with the = (equals) operator also support a wildcard character for pattern matching. The question mark (?) matches any number of characters at the end of a string. The following example

DeptNum= ‘D56’?

is logically true if DeptNum begins with the characters D56. Notice that the question mark must appear outside the pattern string.

If you wish to use the question mark as a literal character, it must be included in the pattern string (i.e., within the quotation marks). The following example

RemarkField = ‘Smith?’

is logically true only if the value of RemarkField is exactly Smith?.

The question mark wildcard can be appended to any atomic expression (i.e., literals, object names, and temporary names) provided that the expression has a character data type (i.e., ALPHA, CHAR, VARALPHA, and VARCHAR).

For example, suppose that LastName is a field in the fCustomer form, having the value Sm//. The expression

Surname = fCustomer.LastName?

is logically true if SurName begins with Sm. The trailing blanks in the pattern are ignored when the question mark is applied to a pattern that is not a literal.

If LastName has the value Sm?//, then the expression

Surname = fCustomer.Lastname

is logically true only if Surname is exactly Sm?.

Using the question mark wildcard in an = (equals) expression is equivalent to using a trailing percent sign wildcard in a LIKE expression. For example, the following expressions are equivalent:

FirstName = ‘ Sm’?

FirstName like ‘ Sm%’

 

Evaluation Order of Functional Expressions

Functional expression are evaluated in stages. Evaluation starts with the most nested argument, and works outwards. If the innermost nest consists of several arguments, the arguments are evaluated from left to right. To change the order of evaluation, use parentheses to change the nesting level.

Consider the following example:

$minof($left(TelNo,3),var1+var2)

Because TelNo is the first argument of the innermost nest, the value of TelNo is determined first. If TelNo is a character-type field whose value is ‘416-555-1212’, then the expression is treated as if it reads

$minof($left(‘416-555-1212’,3),var1+var2)

The other arguments of the innermost nest are literals, so $left is now evaluated, returning the result ‘416’, a character string. The expression is now treated as if it reads

$minof(‘416’,var1+var2)

If the value of var1 is 400 and the value of var2 is 1, then the second argument of $minof evaluates to 401:

$minof(‘416’,401)

Because one of the operands of the $minof function is numeric, all the other operands must be converted. The character string ‘416’ is therefore converted to the number 416:

$minof(416,401)

Finally, the $minof function is applied, returning the result 401.

pt_BRPortuguese