Skip to main content

Subsection 2.4.1 Controlling Execution

Programs without Boolean expressions are like drivers with their eyes closed. Some things may be possible, but without being able to check the environment and respond to it, nothing like the real power of a computer, or a car, can be exploited.

Boolean expressions evaluate to True or False. So they are exactly what we need when we want to describe a procedure that checks relevant conditions and branches based on what it discovers. Once you start to notice this, you’ll see Boolean expressions everywhere.

The following driving procedure contains no explicit Boolean expressions (although, hidden inside some of these commands there probably are such expressions):

Depress accelerator.

Drive for two minutes.

Stop.

Not very useful. Here’s a more effective procedure:

Drive until AtElmStreet.

If LightIsRed OR LightIsYellow:

Stop.

Wait until LightIsGreen.

Turn right.

Drive until AtPetesHouse.

Stop.

We’ve indicated the Boolean expressions in bold. There are five variables (AtElmStreet, LightIsRed, LightIsYellow, LightIsGreen, and AtPetesHouse). Each of them will be either true or false. We’ve also used the OR operator to combine two of them to describe the conditions under which we should stop.

Exercises Exercises

1.

Consider the following procedure for making whipped cream:

Open cream container.

If smell knocks you over:

Stop.

Choose a different dessert. Exit.

Pour cream into bowl.

Beat until peaks form.

Taste.

If not sweet enough:

Add sugar.

Beat a bit more.

Mark the Boolean expressions in this procedure. (Hint: Look for anything that will evaluate to either True or False). How many are there:

  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

Answer.
Correct answer is C.
Solution.
Explanation: There are three: “smell knocks you over”, “peaks form”, and “not sweet enough”. Notice that the last one exploits the Boolean not operator.

2.

Consider the following procedure for doing the dishes:

Until no dirty dishes, do the following:

Pick up a dish.

If it is dishwasher safe, put it in the dishwasher.

Otherwise, wash it by hand.

Run the dishwasher.

When the dishwasher has finished:

Unload and put away the dishes.

Mark the Boolean expressions in this procedure. (Hint: Look for anything that will evaluate to either True or False). How many are there:

  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

Answer.
Correct answer is C.
Solution.
Explanation: There are three: “no dirty dishes”, “it is dishwasher safe”, and “dishwasher has finished”.