Skip to main content

And (& or &&)

Tests whether both its operands have a truth value of TRUE (1).

Details

The And operator 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 evaluates 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).

Examples

The following examples evaluate two nonzero-valued operands as TRUE and produces a value of TRUE (1).

 SET A=-4,B=1
 WRITE A&B // TRUE (1)
 SET A=-4,B=1
 WRITE A&&B // TRUE (1)

The following examples evaluate one true and one false operand and produces a value of FALSE (0).

 SET A=1,B=0
 WRITE "A = ",A,!
 WRITE "B = ",B,!
 WRITE "A&B = ",A&B,! // FALSE (0)
 SET A=1,B=0
 WRITE "A&&B = ",A&&B,! // FALSE (0)

The following examples show the difference between the & operator and the && operator. In these examples, the left operand evaluates to FALSE (0) and the right operand is not defined. The & and && operators respond differently to this situation:

  • The & operator attempts to evaluate both operands, and fails with an <UNDEFINED> error.

      TRY {
           KILL B
           SET A=0
           WRITE "variable A defined?: ",$DATA(A),!
           WRITE "variable B defined?: ",$DATA(B),!
           WRITE A&B
           WRITE !,"Success"
           RETURN
          }
      CATCH exp
      {
          IF 1=exp.%IsA("%Exception.SystemException") {
             WRITE !,"System exception",!
             WRITE "Name: ",$ZCVT(exp.Name,"O","HTML"),!
             WRITE "Data: ",exp.Data,!!
          }
          ELSE { WRITE "not a system exception"}
      }
  • The && operator evaluates only the left operand, and produces a value of FALSE (0).

      TRY {
           KILL B
           SET A=0
           WRITE "variable A defined?: ",$DATA(A),!
           WRITE "variable B defined?: ",$DATA(B),!
           WRITE A&&B
           WRITE !,"Success"
           RETURN
          }
      CATCH exp
      {
          IF 1=exp.%IsA("%Exception.SystemException") {
             WRITE !,"System exception",!
             WRITE "Name: ",$ZCVT(exp.Name,"O","HTML"),!
             WRITE "Data: ",exp.Data,!!
          }
          ELSE { WRITE "not a system exception"}
      }
FeedbackOpens in a new tab