Skip to main content
InterSystems IRIS Data Platform 2024.3
AskMe (beta)
Loading icon

Boolean Values in ObjectScript

This page describes Boolean values in ObjectScript, as well as operators for working with them.

Introduction

Unlike some other languages, ObjectScript does not provide specialized representations of Boolean literal values. Instead:

  • The value 1 and any expression that be interpreted as a nonzero numeric value is considered true. See String-to-Number Conversions for information on how strings are interpreted.

  • The value 0 and any expression that can be interpreted as 0 or that has no numeric interpretation is considered false.

Also, operators that interpret operands as Boolean values return 1 for true or 0 for false.

When you need a simple representation of Boolean values, for simplicity, use 1 for true, and 0 for false.

Uses

Boolean values are most commonly used with:

Logical Operators

The following operators always treat their operands as Boolean values.

Not (')

Not inverts the truth value of the Boolean operand. If the operand is TRUE (1), Not gives it a value of FALSE (0). If the operand is FALSE (0), Not gives it a value of TRUE (1).

For example:

USER>SET x=0

USER>WRITE 'x
1

See the Not (') reference page.

And (& or &&)

And tests whether both its operands have a truth value of TRUE (1). If both operands are TRUE (that is, have nonzero values when evaluated numerically), ObjectScript produces a value of TRUE (1). Otherwise, ObjectScript produces a value of FALSE (0).

There are two forms to And:

  • The & operator evaluates both operands and returns a value of FALSE (0) if either operand evaluates to a value of zero. Otherwise it returns a value of TRUE (1).

  • The && operator evaluates the left operand and returns a value of FALSE (0) if it evaluates to a value of zero. Only if the left operand is nonzero does the && operator then evaluate the right operand. It returns a value of FALSE (0) if the right operand evaluates to a value of zero. Otherwise it returns a value of TRUE (1).

For example.

USER>SET A=-4,B=1
 
USER>WRITE A&&B
1

See the And (& or &&) and Not And (NAND) ('&) reference pages.

Or (! or ||)

Or produces a result of TRUE (1) if either operand has a value of TRUE or if both operands have a value of TRUE (1). Or produces a result of FALSE (0) only if both operands are FALSE (0).

There are two forms to Or:

  • The ! operator evaluates both operands and returns a value of FALSE (0) if both operand evaluates to a value of zero. Otherwise it returns a value of TRUE (1).

  • The || operator evaluates the left operand. If the left operand evaluates to a nonzero value, the || operator returns a value of TRUE (1) without evaluating the right operand. Only if the left operand evaluates to zero does the || operator then evaluate the right operand. It returns a value of FALSE (0) if the right operand also evaluates to a value of zero. Otherwise it returns a value of TRUE (1).

For example:

USER>SET A=5,B=7

USER>WRITE A!B
1
USER>WRITE A||B
1

See the Or (! or ||) and Not Or (NOR) ('!) reference pages.

Precedence and Logical Operators

Because ObjectScript performs a strict left-to-right evaluation of operators, logical comparisons involving other operators must use parentheses to group operations to achieve the desired precedence. For example, you would expect the logical Or (!) test in the following program to return TRUE (1):

  SET x=1,y=0
  IF x=1 ! y=0 {WRITE "TRUE"}
  ELSE {WRITE "FALSE" } 
  // Returns 0 (FALSE), due to evaluation order

However, to properly perform this logical comparison, you must use parentheses to nest the other operations. The following example gives the expected results:

  SET x=1,y=0
  IF (x=1) ! (y=0) {WRITE "TRUE"}
  ELSE {WRITE "FALSE" } 
  // Returns 1 (TRUE)

Combining Boolean Values

You can combine multiple Boolean logical expressions by using logical operators. Like all InterSystems IRIS expressions, they are evaluated in strict left-to-right order. There are two types of logical operators: regular logical operators (& and !) and short-circuit logical operators (&& and ||).

When regular logical operators are used to combine logical expressions, InterSystems IRIS evaluates all of the specified expressions, even when the Boolean result is known before all of the expressions have been evaluated. This assures that all expressions are valid.

When short-circuit logical operators are used to combine logical expressions, InterSystems IRIS evaluates only as many expressions as are needed to determine the Boolean result. For example, if there are multiple AND tests, the first expression that returns 0 determines the overall Boolean result. Any logical expressions to the right of this expression are not evaluated. This allows you to avoid unnecessary time-consuming expression evaluations.

Some commands allow you to specify a comma-separated list as an argument value. In this case, InterSystems IRIS handles each listed argument like an independent command statement. Therefore, IF x=7,y=4,z=2 is parsed as IF x=7 THEN IF y=4 THEN IF z=2, which is functionally identical to the short-circuit logical operators statement IF (x=7)&&(y=4)&&(z=2).

In the following example, the IF test uses a regular logical operator (&). Therefore, all functions are executed even though the first function returns 0 (FALSE) which automatically makes the result of the entire expression FALSE:

LogExp
 IF $$One() & $$Two() {
    WRITE !,"Expression is TRUE."  } 
 ELSE {
    WRITE !,"Expression is FALSE." }
One() 
 WRITE !,"one"
 QUIT 0
Two()
 WRITE !,"two"
 QUIT 1

In the following example, the IF test uses a short-circuit logical operator (&&). Therefore, the first function is executed and returns 0 (FALSE) which automatically makes the result of the entire expression FALSE. The second function is not executed:

LogExp
 IF $$One() && $$Two() {
    WRITE !,"Expression is TRUE."  } 
 ELSE {
    WRITE !,"Expression is FALSE." }
One() 
 WRITE !,"one"
 QUIT 0
Two()
 WRITE !,"two"
 QUIT 1

In the following example, the IF test specifies comma-separated arguments. The comma is not a logical operator, but has the same effect as specifying the short-circuit && logical operator. The first function is executed and returns 0 (FALSE) which automatically makes the result of the entire expression FALSE. The second function is not executed:

LogExp
 IF $$One(),$$Two() {
    WRITE !,"Expression is TRUE."  } 
 ELSE {
    WRITE !,"Expression is FALSE." }
One() 
 WRITE !,"one"
 QUIT 0
Two()
 WRITE !,"two"
 QUIT 1

See Also

FeedbackOpens in a new tab