What's all this about "Boolean" variables?




Document: Software Engineering 1: Course Notes

next Making a statement!
up Chapter 2 pp. 11-19
previous Chapter 2 pp. 11-19

What's all this about "Boolean" variables?

Alcock is at pains to point out that C does not support "Boolean" variables. What does this mean?

Well, certain other programming languages (notably Pascal and its descendants) support a special kind of variable which can only hold or be assigned one of two distinct values - usually referred to as TRUE and FALSE.gif Now Boolean variables would be quite different in kind from the numeric variables we have seen so far in C. Admittedly, as we shall see in more detail later, the total number of distinct values that can be accommodated in any type of C variable (such as type int or type float) is always finite, but it is still much bigger than two!

So why are we worried about all this anyway?

Well, it turns out that, in many programs, it is desirable for the program to have a "choice" of things to do, depending on a variety of factors that only become known when the program is actually executing. In effect this means that we want the program to be able to evaluate some proposition or condition and classify it as being "true" or "false".

To allow this, a programming language normally provides operators to test or compare various conditions - such as whether certain numbers are equal, or one is greater than the other etc. If the language supports Boolean variables as such, then the result of applying or evaluating such an operator would be one of the Boolean values normally denoted TRUE or FALSE. In C however, there is no special Boolean data type, so, instead, the so-called logical operators simply generate numeric results, with the number 0 being interpreted as signalling "false" and the number 1 signalling "true".gif

The logical operators would never generate any value other than 0 or 1. However, where, in effect, a Boolean value is examined or used for some purpose in a C program, any int value may be introduced, with the convention that all non-zero values (including 1 of course) are classified or interpreted as meaning "true".




Document: Software Engineering 1: Course Notes

next Making a statement!
up Chapter 2 pp. 11-19
previous Chapter 2 pp. 11-19



McMullin@ugmail.eeng.dcu.ie
Wed Apr 12 19:40:14 BST 1995