| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
4-5: Boolean Operators
Exercise 4-3: The Check It Button Event code (continued)
One approach is to observe that if "divisible by 4 but not by 100" is true then we know we have a leap year. Only if it is false should we check "divisible by 400", and if that is true then we know we have a leap year. Otherwise we know it is not a leap year.
This suggests the following structure
if (divisible by 4 but not by 100) then
is a leap year
else if (divisible by 400) then
is a leap year
else
is not a leap
year
end if
Another approach is to attempt to combine the first two branches of this If statement. We have "divisible by 4 but not by 100 OR divisible by 400" which could be written as
if (divisible by 4 but not by 100 OR divisible by 400)
then
is a leap year
else
is not a leap year
end if
The difficulty with this approach is that the condition would involve two boolean operators - the AND operator implicit in "divisible by 4 but not by 100" and the OR operator, and this raises the question of which order those operators are applied. If we write
aYear Mod 4 = 0 AND aYear Mod 100 <> 0 OR aYear Mod 400 = 0
does this mean
(aYear Mod 4 = 0 AND aYear Mod 100 <> 0) OR aYear Mod 400 = 0
or does it mean
aYear Mod 4 = 0 AND (aYear Mod 100 <> 0 OR aYear Mod 400 = 0)
or doesn't it make any difference?
As an analogy to this consider the application of the arithmetic operators in an expression such as 2 + 4 * 3 which has different answers depending on whether it means (2 + 4) * 3 or 2 + (4 * 3).
With the boolean operators the result is also different depending on the order of the operations, and as with arithmetic expressions the use of parentheses clarifies any ambiguity. In the absence of parentheses the AND operator is applied before the OR operator, but the use of parentheses in all such situations is highly recommended.