3.4.9 Logical operators

The logical operator provides logical AND && and OR (||).

Organization of this subsection
(1) Format
(2) Description
(3) Example of coding

(1) Format

Logical AND operation

expression-1 && expression-2

Logical OR operation

expression-1 || expression-2

(2) Description

Logical operators do not perform normal arithmetic conversion. Instead, it evaluates the operands in terms of whether they match 0. The result of a logical operation is true or false. The result is the bool type.

The following explains the logical operators.

OperatorDescription
&&If both the operands are true, the result is true. If one of the operands is false, the result is false. If the first operand for logical AND is false, the second operand is not evaluated.
||If both the operands are false, the result is false. If one of the operands is true, the result is true. If the first operand for logical OR is true, the second operand is not evaluated.

The operands for logical AND and OR expressions are evaluated from the left to the right. If the operational result can be identified only by the value for the first operand, the second operand is not evaluated. This is referred to as quick evaluation.

(a) Logical AND operator (&&)

If both the expressions evaluate to true, the result is true. If one of the expressions is evaluates to false, the result is false. The following table shows how the results are evaluated.

Evaluation of expression 1Evaluation of expression 2Resulting evaluation
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse
(b) Logical OR operator (||)

If one or both of the expressions evaluate to true, the result is true. The following table shows how the results are evaluated.

Evaluation of expression 1Evaluation of expression 2Resulting evaluation
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

(3) Example of coding

DEFINE
{
  float varfloat1 = 1.567e-1;
  integer varint1 = 10;
  integer varfloat2 = 0;
  integer varint2 = 0;
  bool varbool;
  integer WINH;
}
MAIN
{
  varbool = varfloat1 && varint1;
  AIT_LogMessage("The expected value of varbool is: true");
  varbool =varfloat2 && varint1;
  AIT_LogMessage("The expected value of varbool is: false");
  varbool=varfloat1 && varint2;
  AIT_LogMessage("The expected value of varbool is: false");
  varbool=varfloat2 && varint2;
  AIT_LogMessage("The expected value of varbool is: false");
}