Back to DFS's Pascal Page


Boolean (Logical) Operators

The Man

George Boole (1815-64) was a self-taught English mathematician who developed an "algebra of logic" which is now called Boolean algebra or Boolean logic.

His Operators

The operators can be used to evaluate the truth value of one or more conditional (or test) statements.There are three operators which we will be concerned with:

AND AND requires that both (all) conditions be TRUE
OR OR requires that at least one of the conditions be TRUE
NOT NOT reverses the truth value - TRUE to FALSE and vice versa

These operators can be used singly or in combination. To understand how they function, it is probably easiest to use logic gates or truth tables, substituting TRUE for 1 and FALSE for 0, as shown below.

The Code

The following code segments illustrate how Boolean conditions and operators can be used to exit from loops and/or programs. The first two code segments perform exactly the same task. The first uses the OR operator while the second uses a combination of NOT and AND. The third shows how to allow the user to exit a program gracefully using the built-in function exit(). Note the use of parens to enclose conditionals when multiples are used.

{ Get a value from the user; accept only a natural number between one and ten, inclusive }
REPEAT
   write('Type a natural number less than or equal to 10 and hit RETURN: ');
   readln(num);
   IF num < 1 THEN
      writeln('Your number is too small!');
   IF num > 10 THEN
      writeln('Your number is too big!')
UNTIL (num >= 1) AND (num <= 10)

{ Get a value from the user; accept only a natural number between one and ten, inclusive } REPEAT write('Type a natural number less than or equal to 10 and hit RETURN: '); readln(num); IF num < 1 THEN writeln('Your number is too small!'); IF num > 10 THEN writeln('Your number is too big!') UNTIL NOT ((num < 1) OR (num > 10)) { Check to see if the user wants to exit the program } { This code segment is usable within a larger main-line loop } REPEAT write('Do you want to exit the program? (Y/N) '); readln(yn); IF (yn = 'Y') OR (yn = 'y') THEN halt UNTIL (yn = 'N') OR (yn = 'n')


© 1996-99 DFStermole
HTMLified: 28 Dec 99