Logical expressions

A logical expression resolves to true or false and is used as a criterion in an if or while statement or (in some situations) in a case statement.

Elementary logical expressions

An elementary logical expression is composed of an operand, a comparison operator, and a second operator, as shown in this syntax diagram and the subsequent table:


Syntax diagram for an elementary logical expression

First operand Comparison Operator Second operand
datetime expression One of these:

=, != , <, >, <=, >=
datetime expression
numeric expression One of these:

=, != , <, >, <=, >=
numeric expression
string expression One of these:

=, != , <, >, <=, >=
string expression
string expression like likeCriterion, which is a character item or literal against which string expression is compared, character position by character position from left to right. Use of this feature is similar to the use of keyword like in SQL queries.

escChar is a one-character item or literal that resolves to an escape character.

For further details, see like operator.

string expression matches matchCriterion, which is a character item or literal against which string expression is compared, character position by character position from left to right. Use of this feature is similar to the use of regular expressions in UNIX or Perl.

escChar is a one-character item or literal that resolves to an escape character.

For further details, see matches operator.

Value of type NUM or CHAR, as described for the second operand One of these:

=, != , <, >, <=, >=
Value of type NUM or CHAR, which can be any of these:

  • An item that is of type NUM and has no decimal places

  • An integer literal

  • An item or literal of type CHAR

searchValue in arrayName; for details, see in.
item not in SQL record One of these:

  • is

  • not

One of these:

  • blanks (for testing whether the value of a character item is or is not blanks only)

  • numeric (for testing whether the value of an item of type CHAR or MBCHAR is or is not numeric)

item in an SQL record One of these:

  • is

  • not

One of these:

  • blanks (for testing whether the value of a character item is or is not blanks only)

  • null (for testing whether the item was set to null either by a set statement or by reading from a relational database)

  • numeric (for testing whether the value of an item of type CHAR or MBCHAR is or is not numeric)

  • trunc (for testing whether non-blank characters were deleted on the right when a single- or double-byte character value was last read from a relational database into the item)

The trunc test can resolve to true only when the database column is longer than the item. The value for the test is false after a value is moved to the item or after the item is set to null.

textField (the name of a field in a text form) One of these:

  • is

  • not

One of these:

  • blanks (for testing whether the value of the text field is or is not limited to blanks or nulls).

    The test for blanks is based on the user's last input to the form, not on the current contents of the form item; and a test that uses is is true in these cases:

    • The user's last input was blanks or null; or

    • The user entered no data in the field since the start of the program or since a set statement ran that was of type set form initial.

  • cursor (for testing whether the user left the cursor in the specified text field).

  • data (for testing whether data other than blanks or nulls is in the specified text field).

  • modified (for testing whether the field's modified data tag is set, as described in Modified data tag and property).

  • numeric (for testing whether the value of an item of type CHAR or MBCHAR is or is not numeric).

sysVar.eventKey One of these:

  • is

  • not

For details, see sysVar.eventKey.
sysVar.systemType One of these:

  • is

  • not

For details, see sysVar.systemType.

You cannot use is or not to test a value returned by sysLib.getVAGSysType.

record name One of these:

  • is

  • not

An I/O error value appropriate for the record organization. See I/O error values.

The next table lists the comparison operators, each of which is used in an expression that resolves to true or false.

Operator Purpose
= The equal operator indicates whether two operands have the same value.
!= The not equal operator indicates whether two operands have different values.
< The less than operator indicates whether the first of two operands is numerically less than the second.
> The greater than operator indicates whether the first of two operands is numerically greater than the second.
<= The less than or equal to operator indicates whether the first of two operands is numerically less than or equal to the second.
>= The greater than or equal to operator indicates whether the first of two operands is numerically greater than or equal to the second.
in The in operator indicates whether the first of two operands is a value in the second operand, which references an array. For details, see in.
is The is operator indicates whether the first of two operands is in the category of the second. For details, see the previous table.
like The like operator indicates whether the characters in the first of two operands is matched by the second operand, as described in like operator.
matches The matches operator indicates whether the characters in the first of two operands is matched by the second operand, as described in matches operator
not The not operator indicates whether the first of two operands is not in the category of the second. For details, see the previous table.

The next table and the explanations that follow tell the compatibility rules when the operands are of the specified types.

Primitive type of first operand Primitive type of second operand
BIN BIN, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT
CHAR CHAR, DATE, HEX, MBCHAR, NUM, TIME, TIMESTAMP
DATE CHAR, DATE, NUM
DBCHAR DBCHAR
DECIMAL BIN, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT
HEX CHAR, HEX
MBCHAR CHAR, MBCHAR
MONEY BIN, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT
NUM BIN, CHAR, DATE, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT, TIME
NUMC BIN, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT
PACF BIN, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT
TIME CHAR, NUM, TIME
TIMESTAMP CHAR, TIMESTAMP
UNICODE UNICODE

Details are as follows:

  • A value of any of the numeric types (BIN, DECIMAL, FLOAT, MONEY, NUM, NUMC, PACF, SMALLFLOAT) can be compared to a value of any numeric type and size, and EGL does temporary conversions as appropriate. An equality comparison of equivalent fractions (like 1.4 and 1.40) evaluates to true, even if the decimal places are different.

  • A value of type CHAR can be compared to a value of type HEX only if each character of type CHAR is within the range of hexadecimal digits (0-9, A-F, a-f). EGL temporarily converts any lowercase letters to uppercase in the value of type CHAR.

  • If a comparison includes two values of character type (CHAR, DBCHAR, HEX, MBCHAR, UNICODE) and one value has fewer bytes than the other, a temporary conversion pads the shorter value on the right:

    • In a comparison with a value of type MBCHAR, a value of type CHAR is padded on the right with single-byte blanks

    • In a comparison with a value of type HEX, a value of type CHAR is padded on the right with binary zeros

    • A value of type DBCHAR is padded on the right with double-byte blanks

    • A value of type UNICODE is padded on the right with Unicode double-byte blanks

    • A value of type HEX is padded on the right with binary zeros, which means (for example) that if a value "0A" must be expanded to two bytes, the value for comparison purposes is "0A00" rather than "000A"

  • A value of type CHAR can be compared to a value of type NUM only if these conditions apply:

    • The value of type CHAR has single-byte digits, with no other characters

    • The definition of the value of type NUM has no decimal point

    A CHAR-to-NUM comparison works as follows:

    • A temporary conversion puts the NUM value into a CHAR format. The numeric characters are left-justified, with additional single-byte blanks as needed. If a NUM-type item of length 4 has a value of 7, for example, the value is treated as "7" with three blanks on the right.

    • If the length of the items do not match, a temporary conversion pads the shorter value with blanks on the right.

    • The comparison checks the values byte-by-byte. Consider two examples:

      • A CHAR-type item of length 2 and value "7 " (including a blank) is equal to a NUM-type item of length 1 and value 7 because the temporary item that is based on the NUM-type field also includes a final blank

      • A CHAR-type item of value "8" is greater than a NUM-type item of value of 534 because the "8" comes after "5" in the ASCII or EBCDIC search order

Complex logical expressions

You can build a more complex expression by using either an and (&&) or or operator (||) to combine a pair of more elementary expressions. In addition, you can use the not operator (!), as described later.

If a logical expression is composed of elementary logical expressions that are combined by or operators, EGL evaluates the expression in accordance with the rules of precedence, but stops the evaluation if one of the elementary logical expressions resolves to true. Consider an example:

  item01 = item02 || 3 in array03 || x = y

If item01 does not equal item02, evalution proceeds. If the value 3 is in array03, however, the overall expression is proven to be true, and the last elementary logical expression (x = y) is not evaluated.

Similarly, if elementary logical expressions are combined by and operators, EGL stops the evaluation if one of the elementary logical expressions resolves to false. In the following example, evaluation stops as soon as item01 is found to be unequal to item02:

  item01 = item02 && 3 in array03 && x = y

You may use paired parentheses in a logical expression for any of these purposes:

  • To change the order of evaluation.

  • To clarify your meaning.

  • To make possible the use of the not operator (!), which resolves to a Boolean value (true or false) opposite to the value of a logical expression that immediately follows. The subsequent expression must be in parentheses.

Examples

In reviewing the examples that follow, assume that value1 contains "1", value2 contains "2", and so on:

   /* = true */
   value5 < value2 + value4

   /* = false */
   !(value1 is numeric)

   /* = true when the generated output runs 
             on Windows 2000, Windows NT, 
             or z/OS UNIX System Services */
   sysVar.systemType is WIN || sysVar.systemType is USS

   /* = true */
   (value6 < 5 || value2 + 3 >= value5) && value2  = 2

Related concepts
Java access functions
Modified data tag and modified property

Related tasks
Syntax diagram

Related reference
case
Datetime expressions
Exception handling
Expressions
I/O error values
if, else
in operator
Items
like operator
matches operator
Numeric expressions
Operators and precedence
Primitive types
Text expressions
sysVar.eventKey
sysLib.getVAGSysType
sysVar.systemType
while