IDL supports three logical operators: &&, ||, and ~. When working with logical operators, non-zero numerical values, non-empty strings, and non-empty heap variables (pointers and object references) are considered true, everything else is false.

Note: Programmers familiar with the C programming language, and the many languages that share its syntax, may expect ~ to perform bitwise negation (1’s complement), and for ! to be used for logical negation. This is not the case in IDL: ! is used to reference system variables, the NOT operator performs bitwise negation, and ~ performs logical negation.

Note: If one or both of the operands are objects, the operator may be overloaded. See Object Operator Overloading Overview for more information.

Operator

Description

Example

&&

Logical AND

Returns true (1) whenever both of its operands are true; otherwise, returns false (0). Non-zero numerical values, non-empty strings, and non-null heap variables (pointers and object references) are considered true, everything else is false.

Operands must be scalars or single-element arrays. The && operator short-circuits; the second operand will not be evaluated if the first is false. See Short-circuiting for details.

PRINT, 5 && 7

IDL Prints:

1

PRINT, 5 && 2

IDL Prints:

1

PRINT, 4 && 0

IDL Prints:

0

PRINT, "" && "sun"

IDL Prints:

0                            

||

Logical OR

Returns true (1) whenever either of its operands are true; otherwise, returns false (0). Uses the same test for “truth” as the && operator.

Operands must be scalars or single-element arrays. The || operator short-circuits; the second operand will not be evaluated if the first is true. See Short-circuiting for details.

IF ((5 GT 3) || (4 GT 5)) $
   THEN PRINT, 'True'
 

IDL Prints:

True

~

Logical negation

Returns true (1) when its operand is false; otherwise, returns false (0).
Uses the same test for “truth” as the && operator.

 

For lists and hashes, the logical negation operator returns true (1) if the list or hash is empty; otherwise it returns false (0).

PRINT, ~ [1, 2, 0]
 

IDL Prints:

0  0  1

 

 

list = LIST()
IF (~list) $
   THEN PRINT, 'Empty List'
 

IDL Prints:

Empty List

Short-circuiting


The && and || logical operators, added to IDL in version 6.0, are short-circuiting operators. This means that IDL does not evaluate the second operand unless it is necessary in order to determine the proper overall answer. Short-circuiting behavior can be powerful, since it allows you to base the decision to compute the value of the second operand on the value of the first operand. For instance, in the expression:

Result = Op1 && Op2 

IDL does not evaluate Op2 if Op1 is false, because it already knows that the result of the entire operation will be false. Similarly in the expression:

Result = Op1 || Op2 

IDL does not evaluate Op2 if Op1 is true, because it already knows that the result of the entire operation will be true.

To ensure that both operands are evaluated (perhaps because the operand is an expression that changes value when evaluated), use the LOGICAL_AND and LOGICAL_OR functions or the bitwise AND and OR operators.

Additional Logical Operator Examples


Results of relational expressions can be combined into more complex expressions using the logical operators. Some examples of relational and logical expressions are as follows:

;True if A is between 25 and 50. If A is an array, then the result 
;is an array of zeros and ones.
(A LE 50) && (A GE 25)
 
;True if A is less than 25 or greater than 50. This is the inverse 
;of the first.
(A GT 50) || (A LT 25)