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

The first and second expressions must be of compatible types.

In the case of datetime comparisons, the greater than sign (>) means later in time; and the less than (<) sign means earlier in time.

numeric expression One of these:

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

==, != , <, >, <=, >=
string expression
string expression like likeCriterion, which is a character field 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 field or literal that resolves to an escape character.

For further details, see like operator.

string expression matches matchCriterion, which is a character field 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 field 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:
  • A field that is of type NUM and has no decimal places
  • An integer literal
  • A field or literal of type CHAR
searchValue in arrayName; for details, see in.
field not in SQL record One of these:
  • is
  • not
One of these:
  • blanks (for testing whether the value of a character field is or is not blanks only)
  • numeric (for testing whether the value of a field of type CHAR or MBCHAR is or is not numeric)
field in an SQL record One of these:
  • is
  • not
One of these:
  • blanks (for testing whether the value of a character field is or is not blanks only)
  • null (for testing whether the field was set to null either by a set statement or by reading from a relational database)
  • numeric (for testing whether the value of a field 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 field)

The trunc test can resolve to true only when the database column is longer than the field. The value for the test is false after a value is moved to the field or after the field 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 field; 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 a field of type CHAR or MBCHAR is or is not numeric).
ConverseVar.eventKey One of these:
  • is
  • not
For details, see ConverseVar.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 VGLib.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 equality 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, TIMESTAMP
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
TIMESTAMP CHAR, DATE, TIME, 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 a fixed character type (CHAR, DBCHAR, HEX, MBCHAR, UNICODE, limited-length STRING) 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 or limited-length STRING is padded on the right with single-byte blanks
    • In a comparison with a value of type HEX, a value of type CHAR or limited-length STRING 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"
  • Any trailing blanks in a limited-length string are ignored during the comparison itself, after any padding occurs. Trailing blanks in other fields of type STRING, however, are not ignored.
  • 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 field 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 fields 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 field of length 2 and value "7 " (including a blank) is equal to a NUM-type field of length 1 and value 7 because the temporary field that is based on the NUM-type field also includes a final blank
      • A CHAR-type field of value "8" is greater than a NUM-type field 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:
  field01 == field02 || 3 in array03 || x == y

If field01 does not equal field02, 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 field01 is found to be unequal to field02:

  field01 == field02 && 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
Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.