And

From BR Wiki
Jump to navigation Jump to search

And is a logical operator which evaluates a combination of two logical conditions.

Suppose that we wish to ensure at some point in an application that two conditions are both true before we choose a certain path of execution. In this case, we can use the AND operator, as follows:

00010 if sex == "M" AND age >= 65 then seniorMales += 1;

This if statement contains two simple conditions. The condition sex == "M" determines whether a person is male. The condition age >= 65 might be evaluated to determine whether a person is a senior citizen. The if statement considers the combined condition

sex == "M" AND age >= 65

which is true if and only if both simple conditions are true. If the combined condition is true, the if statement’s body increments seniorMales by 1. If either or both of the simple conditions are false, the application skips the increment. Some programmers find that the preceding combined condition is more readable when redundant parentheses are added, as in:

( sex == "M" ) AND ( age >= 65 )

The table below summarizes the AND operator. The table shows all four possible combinations of false and true values for expression1 and expression2. Such tables are called truth tables.

Operator AND has a higher precedence than operator OR. Both operators associate from left to right.