Chapter 3- Data Types and Operators Boolean Expressions Page 2 3 4

Introduction

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)

Turning now to the first block of the If statement; it is executed when the textbox does contain a string that can be converted to a date value. Having obtained the date value we must extract the year from that value and then determine if the year is a leap year. To do this we must first determine if the year is divisible (with remainder being zero) by 4 but not by 100. Essentially we want to see if there is any remainder when we divide the year by 4 or by 100. This introduces a new arithmetic operator - the Mod operator.

The Mod operator divides its first operand by the second and creates a result that is the value of the remainder. Thus 12 mod 5 has the result 2, for example. If the result from the mod operation is 0 then the first operand is divisible (without remainder) by the second. For example, 16 mod 8 has the result 0.

In this exercise we need to write the boolean expressions
Year Mod 4 = 0
meaning that the value of year is divisible by 4, and
Year Mod 100 <> 0
meaning that the value of year is not divisible by 100, and combine these with a boolean operator. But which operator should it be?

If this first expression is false (i.e. divisible by 4 but not by 100 is false) it is still possible for the year to be a leap year if it is divisible by 400. There are a number of ways in which this logic maybe implemented.