And: Difference between revisions
(edit) |
No edit summary |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
AND | '''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. | |||
[[File:AND_truthtable.PNG]] | |||
'''Operator AND has a higher precedence than operator [[OR]]. Both operators associate from left to right.''' | |||
<noinclude> | <noinclude> | ||
[[Category: | [[Category:Operations]] | ||
[[Category:Logical | [[Category:Logical Operations]] | ||
</noinclude> | </noinclude> |
Latest revision as of 13:50, 13 May 2014
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.