< previous page page_190 next page >

Page 190
Notice that we have capitalized the identifier Boolean according to the style we described in Chapter 2. We begin our variable names with lowercase letters, and we begin the names of programmer-written functions and programmer-defined data types with uppercase letters.
To complete the construction of our own Boolean type in C++, we define two named constants, TRUE and FALSE:
typedef int Boolean;
const Boolean TRUE = 1;
const Boolean FALSE = 0;
  .
  .
  .
Boolean dataOK;
  .
  .
  .
dataOK = TRUE;
  .
  .
  .
dataOK = FALSE;
As you can see, we haven't really created a new data type. Boolean is just a synonym for int, and TRUE and FALSE are synonyms for 1 and 0. Why do we go to the trouble of doing this? First, it helps us to mentally separate int data (numeric values) from Boolean data (truth values) as we design and implement programs. Second, we end up with self-documenting code. Someone looking at our programs can see right away that the identifiers Boolean, TRUE, and FALSE refer to logical (Boolean) data and not integer values.
Throughout the rest of this text, whenever we need Boolean data in a C++ program, we incorporate the following statements into the program:
typedef int Boolean;
const Boolean TRUE = 1;
const Boolean FALSE = 0;
Relational Operators
One way of assigning values to Boolean variables is to use an assignment statement, like this:
itemFound = TRUE;
We also can assign values to Boolean variables by setting them equal to the result of comparing two expressions with a relational operator. Relational operators test a relationship between two values.
Let's look at an example. In this program fragment, lessThan is a Boolean variable and i and j are int variables:

 
< previous page page_190 next page >