Writing Logical Expressions

In this section:

A logical expression determines whether a particular condition is true. There are two kinds of logical expressions, relational and Boolean. The entities you wish to compare determine the kind of expression.

A relational expression returns TRUE or FALSE based on comparison of two individual values (either variables or constants). A Boolean expression returns TRUE or FALSE based on the outcome of two or more relational expressions.

You can use a logical expression to assign a value to a numeric variable. If the expression is true, the variable receives the value 1. If the expression is false, the variable receives the value 0.


Top of page

x
Relational Expressions

A relational expression returns TRUE or FALSE based on the comparison of two individual values (either variables or constants). The following syntax lists the operators you can use in a relational expression:

character_expression  char_operator  character_constant 
numeric_expression  numeric_operator  numeric_constant

where:

char_operator

Can be any of the following: EQ, NE, OMITS, CONTAINS.

numeric_operator

Can be any of the following: EQ, NE, LE, LT, GE, GT.


Top of page

x
Boolean Expressions

How to:

Boolean expressions return a value of true (1) or false (0) based on the outcome of two or more relational expressions. Boolean expressions are often used in conditional expressions, which are described in Writing Conditional Expressions. You can also assign the result of a Boolean expression to a numeric or character variable, which will be set to 1 (if the expression is true) or 0 (if it is false). They are constructed using variables and constants connected by operators.



x
Syntax: How to Use Boolean Expressions

The syntax of a Boolean expression is:

(relational_expression) {AND|OR} (relational_expression)
NOT (logical_expression)

Boolean expressions can themselves be used as building blocks for more complex expressions. Use AND or OR to connect the expressions and enclose each expression in parentheses.


Top of page

x
Evaluating Logical Expressions

Reference:

If you assign a Boolean expression to a character variable, it may have the values TRUE, FALSE, 1, or 0. TRUE and 1 are equivalent, as are FALSE and 0. A numeric variable may have the values 1 or 0.

Alphanumeric constants with embedded blanks used in the expression must be enclosed in single quotation marks. An example is:

IF NAME EQ 'JOHN DOE'

OR cannot be used between constants in a relational expression. For example, the following expression is not valid

IF COUNTRY EQ 'US' OR 'BRAZIL' OR 'GERMANY'

Instead, it should be coded as a sequence of relational expressions:

IF (COUNTRY EQ 'US') OR (COUNTRY EQ 'BRAZIL') OR (COUNTRY EQ 'GERMANY')


x
Reference: Logical Operators

The following list shows the logical operators you can use in an expression:

Description

Operator

Equality

EQ

Inequality

NE

Less than

LT

Greater than

GT

Less than or equal to

LE

Greater than or equal to

GE

Contains the specified character string

CONTAINS

Omits the specified character string

OMITS

Negation

NOT

Conjunction

AND

Disjunction

OR

Boolean operators are evaluated after numeric operators from left to right in the following order of priority:

Order

Operators

1

EQ NE LE LT GE GT NOT CONTAINS OMITS

2

AND

3

OR

WebFOCUS