3.1 Boolean Expressions

Turing has another type that we haven't mentioned yet. The name of the type is boolean. Boolean types can only take on one of two possible values. Those values are true and false. Turing (and virtually all programming languages) uses this type to make decisions. By making decisions a program can do different things depending on what input it receives.

You can use variables of type boolean like you used the other types we've seen before. If you are getting the input you must keep in mind that typing anything other than true or false will crash your program (that's why many languages don't allow you to read values into boolean variables).

Here is an example program that uses boolean variables (although not for any useful purpose yet):

var isStudent : boolean := true
var isVoter : boolean := false

put "You are a student: ", isStudent
put "You are a voter: ", isVoter 

The output is:

You are a student: true
You are a voter: false

Most of the time we do not actually use boolean variables nor do we assign true and false directly. We will usually generate our boolean expressions by making comparisons. Most of the comparison operators are the same as you would use in mathematics. Here are the comparison operators and what they mean:

Comparison OperatorMeaning
=
is equal to
not=
is not equal to
<
is less than
<=
is less than or equal to
>
is greater than
>=
is greater than or equal to

The operators >= and <= must be written in that order (ie. => and =< are illegal) and there cannot be any spaces between the two characters. You can write not= with a space between the not and the =. If you reverse the order you will not generate a syntax error, but you will not get the results you expect (8 = not 10 gives false. This is because not 10 = 4294967285. This has to do with the way numbers are stored in the computer using the binary system. We don't want to get into the details here but suffice it to say that x = not y will almost always be false).

Here are some examples of comparisons:

StatementResult
10 < 15 true
-3.4 >= -2.3false
"one" = "ONE" false
5 + 3 > 2 * (20 - 18) true
-5 <= -5 true
4.6 > 5 false
11 not= 11 false

You can compare int and real types to each other but otherwise only compare two values of the same type. In other words don't compare strings with numbers. When you compare strings lower case letters and upper case letters are not considered the same (as seen in the previous example). Upper case letters are considered to come before lower case letters (eg. "A" < "a" - we'll see more detail about this later). Boolean values can only be compared using = and not=.

Exercise 3.1

  1. For each legal expression, state its value. For each illegal expression, state the reason why it is illegal.
    1. (2 + (-5)) not= 3
    2. 'm' < = 'p'
    3. (25 mod 4) >= 1
    4. "**" < "**"
    1. 8.23 =< 8.2300
    2. true = "true"
    3. 5 not = 5
    4. 5 >= 5
  2. What will this program print? Justify your answer.
    var perhaps, maybe : boolean
    
    perhaps := 4 < 5
    maybe := (1 <= 2)
    put perhaps, " ", maybe
    put perhaps = ('x' not= 'y')
    put ('G' = 'g') = (maybe)
    put perhaps, " ", maybe 
  3. State the value of each expression below and verify your answers by writing a program that will print the value of each expression.
    1. 'D' <= 'F'
    2. (-17 mod 4) = 1
    1. 6.4 = 8 ** 2 div 10
    2. (10 div 2) = (10 / 2)