Was this helpful?
Logical Expressions
Logical expressions are expressions yielding the Boolean values True, False, or Unknown (for NULL values). 4GL includes the Boolean comparison operators and the logical operators AND, OR, and NOT.
Boolean Comparison Operators
The Boolean comparison operators join numeric or string expressions into a logical expression. The following table summarizes the Boolean comparison operators:
Operator
Description
Left-hand Operand
Right-hand Operand
=
Equal to
Numeric or string expression
Numeric or string expression
!=
<>
^=<
Not equal to
Numeric or string expression
Numeric or string expression
<
Less than
Numeric or string expression
Numeric or string expression
<=
Less than or equal to
Numeric or string expression
Numeric or string expression
>
Greater than
Numeric or string expression
Numeric or string expression
>=
Greater than or equal to
Numeric or string expression
Numeric or string expression
IS NULL
Value is "null"
Numeric or string expression
None
IS NOT NULL
Value is other than "null"
Numeric or string expression
None
IS INTEGER
Value is an integer
Numeric or string expression
None
IS NOT INTEGER
Value is other than an integer
Numeric or string expression
None
IS DECIMAL
Value is a decimal type
Numeric or string expression
None
IS NOT DECIMAL
Value is other than a decimal type
Numeric or string expression
None
IS FLOAT
Value is a float type
Numeric or string expression
None
IS NOT FLOAT
Value is other than a float type
Numeric or string expression
None
LIKE
Value matches pattern-matching string
String expression
See The LIKE Operator and Pattern Matching
NOT LIKE
Value does not match pattern-matching string
String expression
See The LIKE Operator and Pattern Matching
The result of a comparison with a nullable value can be True, False or Unknown. The result is Unknown if one or both operands of the expression are NULL, except for the IS NULL and IS NOT NULL operations.
4GL tests the resulting expressions in if and while statements. 4GL also uses them in the where clauses of query statements.
The condition that a 4GL if or while statement tests must be a Boolean expression. For example, if Status is a character field in the form currently being displayed, then the following condition yields a Boolean value:
status = 'n'
The if statement below leads to a call on NewProject when the Status field has "n" as its value:
if status = 'n' then
    callframe NewProject;
endif;
In if and while statements, if the result of an expression is Unknown, then the flow of control occurs exactly as if the Boolean expression evaluates to False. For examples, see Nulls in Expressions.
The LIKE Operator and Pattern Matching
4GL allows you to compare two strings to see whether they resemble each other in specific ways.
To do this, specify a pattern—a string of characters with special formatting characters—that shows what the compared string must look like, along with the LIKE pattern-matching operation. If the string you specify is an instance of the pattern string, then the comparison evaluates to True. The syntax for the LIKE operation is as follows:
variable [not] like pattern [escape escapechar]
Variable is the name of a character string field or variable, pattern is a character string literal or character string variable, and escapechar is a character string literal or character string variable of length 1. Pattern must not be NULL.
Pattern strings can include the following special characters:
An underscore (_) is a "wildcard" that matches any single character. For example, "Xa," "aa" and "/a" are all text strings that match the pattern string "_a."
A percent sign (%) is a "wildcard" that matches any string of characters, regardless of length. For example, the string "Fred%" can retrieve the pattern strings "Fred," "Frederick," and "Fred S. Smith, PhD."
Square brackets [ ] when preceded by an escape character match the corresponding character position with any of the characters within the bracketed string. Examples are given below with a further description of this feature.
In comparisons using the arithmetic operators, pattern-matching characters do not have any effect. For example, in the following statement, the comparison tests the value of name to see whether it exactly equals the constant literal "Leslie%." If it does, the object of the conditional is executed.
if name = 'Leslie%' then ...
To make the pattern-matching characters take on their special meanings, use the LIKE operator as any other comparison operator is used. For example, the following statement tests whether a text value ends in the letter e:
if name like '%e' then
    message 'Found a name ending in e';
endif;
The following statement tests the value of variable emp_name against the pattern, to see if it starts with "Leslie", and it makes sure that the last name of the employee is "Smith":
if emp_name like 'Leslie%Smith' then
    message 'Found a name matching the pattern'
endif;
You can use the escape clause with the LIKE operator in two ways.
Use the escape clause to "escape" the special interpretation of the pattern matching characters "_" and "%." For example, the following phrase matches a string with any first character and an underline as the second character:
name LIKE '_\_' ESCAPE '\'
The next phrase matches any string beginning with a period (.) and ending with a percent sign:
name LIKE '..%.%' ESCAPE '.'
Use the escape clause to give a special interpretation to square brackets [ ] in the pattern string. Normally, square brackets are treated like any other characters. However, when preceded by the escape character, the brackets "escape" their standard interpretation and can define a match-any-of-these-characters string.
For example, suppose you want to match all strings ending with X, Y or Z. Use the following phrase:
name LIKE '%\[XYZ\]' ESCAPE '\'
In the following example, the pattern matches "ABC," "ACC," and "FCC Fairness Doctrine," and does not match "FDC Yellow #42" or "Access."
name LIKE '_\[BC\]C%' ESCAPE '\'
The next example matches any string beginning with a left bracket and whose second character is "1," "2," "3" or a right bracket:
name LIKE '[\[123]\]' ESCAPE '\'
An escape character must be followed in the pattern by an underscore, percent, left or right bracket, or another escape character.
AND, OR, and NOT Operators
The logical operators (AND, OR, and NOT) join logical expressions into new logical expressions. The following truth tables show the result of comparisons made with the AND, OR, and NOT operators:
The following table lists the AND operators:
True
False
Unknown
True
True
False
Unknown
False
False
False
False
Unknown
Unknown
False
Unknown
The following table lists the OR operators:
True
False
Unknown
True
True
True
True
False
True
False
Unknown
Unknown
True
Unknown
Unknown
The following table lists the NOT operators:
True
False
Unknown
False
True
Unknown
The example below illustrates the use of the IF and AND operators:
if empnum > 0 and status != 3 then 
    callframe newemployee; 
endif;
The frame newemployee is called only if both conditions are True. The current value of empnum must be greater than zero and status must have any value other than 3.
This example illustrates the use of the NOT and OR operators:
if not (status = 1 or status = 2) then 
    callframe newemployee; 
endif;
The frame newemployee is called if neither of the conditions are True. The current value of status cannot be 1 or 2.
Precedence of Logical Operators
The logical operators have the following precedence (in descending order):
NOT
AND or OR
Precedence is handled as follows:
In the absence of parentheses:
NOT is done before AND or OR.
Adjacent occurrences of AND and OR are done from left to right.
If there are any operators within parentheses, these are done, beginning with the innermost parentheses, before any operators outside of parentheses.
You can use parentheses to change the default order of evaluation, or simply to make it more explicit; for example, the following expression:
x = a OR x = b AND NOT y = c
is equivalent to:
(x=a OR x=b) AND (NOT y=c)
The order of precedence is different for 4GL logical operators than for SQL logical operators. To avoid confusion when using logical operators in 4GL, use parentheses in expressions that use a mixture of the operators.
Last modified date: 04/03/2024