ISTRUE operator

Purpose | Return the logical truth or falsity of a given expression.
Syntax | ISFALSE expr ISTRUE expr

Remarks

ISTRUE returns -1 (TRUE) when expr evaluates as non-zero; otherwise, it returns zero (FALSE). ISFALSE returns -1 when expr evaluates as 0 (FALSE); otherwise, it returns zero.

Truth table
operator | expr | Result
ISTRUE | = 0 | 0
ISTRUE | <> 0 | -1
ISFALSE | = 0 | -1
ISFALSE | <> 0 | 0

PowerBASIC's NOT operator serves a double duty: it returns the one's-complement of an integer class expression and "reverses" the value of a TRUE/FALSE (Boolean) expression. Usually, these two functions do not conflict, but since PowerBASIC accepts any non-zero value as TRUE, the following condition can arise:

test1 = 0 ' test1 is FALSE (zero)

IF NOT test1 THEN ' TRUE (-1 is non-zero)

[statements]

test2 = 1 ' test2 is TRUE (1 is non-zero)

IF NOT test2 THEN ' still TRUE (-2 is non-zero)

[statements]

In this case, NOT does not reverse the TRUE/FALSE value of test2. ISFALSE ensures that the test is performed exactly as you would expect:

test2 = 1 ' test2 is TRUE (non-zero)

IF ISFALSE test2 THEN ' ISFALSE detects test2 is

[statements] ' TRUE so the IF test fails

This problem does not exist when you're testing for logical truth. PowerBASIC considers that an expression is TRUE in every case except when the expression is zero. However, ISTRUE converts all non-zero values to the "most true" value, -1, which provides the most consistent results with both boolean and arithmetic expressions.

Restrictions

ISTRUE and ISFALSE operators evaluate the "whole" expression following the keyword, subject to their Operator Precedence level. For example, parentheses contained within the expression are regarded as an integral part of the expression, and do not act as delimiters for the ISTRUE and ISFALSE operators.

With this in mind, combining a logical test result into a further expression means that the expressions must be separated to ensure the correct evaluation.

Consider the following statement:

IF ISTRUE (x&) + y& THEN

PowerBASIC evaluates the entire expression (x&) + y& and then calculates the logical truth from the overall result of that expression. That is, the parentheses around the first part of the expression do not stop ISTRUE from evaluating the whole expression. To demonstrate this, the statement can be rewritten to concisely demonstrate the scope of the logical evaluation:

IF ISTRUE (x& + 2) THEN

or it could be simplified even further:

IF ISTRUE x& + 2 THEN

If you wish to utilize the numeric result of the logical test in a further expression, parentheses must be added to separate the expressions correctly:

IF (ISTRUE x&) + 2 THEN

See also | Arithmetic Operators, NOT, Short-circuit evaluation