Logical Data (Booleans)

All data has a logical (or Boolean) value, which is to say that it can be computed as true or false. If the data contains only numeric values and the numeric value is zero (0), it is false; any other numeric value is true. If the data contains character string values other than the null string (""), it is true; the null string is false. Logical values are used for testing conditionals. If a statement reads:

IF FOUND THEN...

the variable FOUND is tested to see if it is not null or zero. If it is not, then the condition is determined to be true, and the statements following the THEN clause are executed.

Relational Operators

Relational operators are used to compare both numeric and character string data. The result of the comparison, either true (1) or false (0), can be used for conditional statements. The relational operators are:

Operator

Relation

Example

EQ or =

Equality

X = Y

NE or #

Inequality

X # Y

>< or <>

Inequality

X <> Y

LT or <

Less than

X < Y

GT or >

Greater than

X > Y

LE or <= or =<

Less than or equal to

X <= Y

GE or >= or => or #<

Greater than or equal to

X >= Y

When arithmetic and relational operators are both used in a single expression, the arithmetic operation is always performed first.

The same relational operators can be applied to both numeric and string data, but the operations will be calculated differently according to the type of data in the operands. Relational numeric comparisons are calculated as expected, by comparing the literal value of the operands. String comparisons, however, are made by comparing the ASCII values of single characters from each string.

In string comparisons, characters are compared from left to right, and the first string to yield a higher numeric ASCII code equivalent is considered to be greater. If all of the ASCII codes are the same, the strings are considered equal. If the two strings have different lengths but the shorter string is otherwise identical to the beginning of the longer string, the longer string is considered greater than the shorter string. Note that leading and trailing blank spaces are significant, since the space character has an ASCII value of 032.

If both string values can be converted to numeric, then the comparison is always made numerically. If only one operand is numeric, the comparison will be made as if both were string values.

The following string comparisons are true and return a value of 1:

"AA" < "AB"

"FILENAME" = "FILENAME"

"FILENAME" < "NAMEFILE"

"CL  " > "CL"

"KG" > " KG"

"8/12/78"< "9/12/78"

Logical Operators

Logical operators perform Boolean operation tests on logical expressions. They have the lowest precedence among all operators: they are evaluated after all other operators have been evaluated.

The two forms of logical operation in mvBASIC are:

Operator

Syntax

Definition

AND

x AND y

True (evaluates to 1) if both x and y are true.

&

x & y

True (evaluates to 1) if both x and y are true.

OR

x OR y

True (evaluates to 1) if either x or y is true.

!

x ! y

True (evaluates to 1) if either x or y is true.

The NOT function can be used to invert a logical value.

For example:

IF FOUND AND QUIT = "Y" THEN...

The variable FOUND is tested to see if it is not null or zero, and is evaluated as true if it is not. Then the relational expression QUIT = "Y" is evaluated. If both FOUND and QUIT = "Y" are evaluated as true, then the condition as a whole is evaluated as true and the statements following the THEN clause are executed.

The MATCH Operator

The pattern matching operator, MATCH or MATCHES, can be used to compare a string expression to a pattern specification and return a value of 1 if they match. The syntax for a MATCH operation is:

expr MATCH pattern-expr

The pattern is a general description of the format of the string and can be specified as a constant or as an expression. The pattern specification codes and their definitions are as follows:

Pattern

Definition

nN

n numeric characters.

nA

n alphabetic characters.

nX

n wildcards (any character).

string

Any literal string.

n must be a whole number. If n is 0, the relation is true only if all the characters match the specified type. (Note that the null string ("") matches 0N, 0A, and 0X.)

For example,

ZIP.CODE MATCH "0N"

will be true only if all the characters in the string ZIP.CODE are digits. Patterns can be combined in any sequence.

For example,

IF LICENSE MATCHES "3N'-'3A" THEN...

confirms that a license number entered consists of 3 digits, a dash, and 3 alphabetic letters.

Logical Functions

Logical functions are functions that return a value of 0 or 1. The following are the logical functions available in mvBASIC:

Function

Description

ALPHA

Tests the given expression for an alphabetical value.

NOT

Returns the logical inverse of a given expression.

NUM

Tests the given expression for a numeric value.

See Statement and Function Reference for a full description of the syntax and behavior of these functions.

See Also

Building Expressions

Simple Assignment

Using Operators and Functions

Numeric Expressions

String Expressions