< previous page page_540 next page >

Page 540
cion from int to an enumeration type is not defined, so we cannot store an int value into a Boolean variable. Therefore, it is better not to define Boolean as an enumeration type but to use a Typedef along with const declarations for TRUE and FALSE.
Incrementation
Suppose you want to ''increment" the value in inPatient so that it becomes the next value in the domain:
inPatient = inPatient +1;   //  No
This statement is illegal for the following reason. The right-hand side is okay because implicit type coercion lets you add inPatient to 1; the result is an int value. But the assignment operation is not valid because you can't store an int value inPatient. The statement
inPatient++;   // No
is also invalid because the compiler considers it to have the same semantics as the assignment statement above. However, you can escape the type coercion rule by using an explicit type conversiona type castas follows:
inPatient = Animals (inPatient +1);   // Yes
When you use the type cast, the compiler assumes that you know what you are doing and allows it.
Incrementing a variable of enumeration type is very useful in loops. Sometimes we need a loop that processes all the values in the domain of the type. We might try the following For loop:
Animals patient;

for (patient=RODENT; patient <= SHEEP; patient++) // No
    .
    .
    .
However, as we explained above, the compiler will complain about the expression patient++. To increment patient, we must use an assignment expression and a type cast:
for (patient=RODENT; patient <= SHEEP; patient=Animals(patient + 1))
    .
    .
    .

 
< previous page page_540 next page >