Making decisions

Computers can't decide

Because computers can't think, they can't make decisions by themselves. That's why they need rules to guide them.

OK computer:
rule #1: there are no rules

As you may have expected, these rules have to be a lot more precise ... and stringent.

Conditions

Computer rules depend on conditions, another word for results of evaluations.

Which of these conditions might help you determine if you need to go to work?

  • You've got a job
  • It's a weekday
  • You're not on vacation
  • You're late for the bus

Sweet except the last! If these conditions are met, it's probably time to go to work.

If-then

Computers use words like if and then to guide their behavior.

OK computer:
if it is before 12pm
then display "Good morning!"

Sweet! That's a precise but straight-forward rule, right?

If-then-else

So, what if we want some other thing to happen when the condition isn't met? There's a word for that and it's called else.

OK computer:
if it is before 12pm
then display "Good morning!"
else display "Good day!"

Sweet! The ability to follow different paths in different situations is what makes computers seem intelligent.

Doors unlocked and open

In essence, rules make programs more intelligent.

Do you remember the door sequence from earlier?

unlock the door
open the door
enter the room
close the door

Well done! Now, what if the door was unlocked from the start? And what if it was open?

Ruling the doors

Let's take care of that. Which of these rules are necessary to make the sequence more intelligent?

  • if the door is locked
    then unlock it
  • if the door is closed
    then open it
  • if the door is open
    then enter the room
  • if you are in the room
    then close the door

Nailed it if you chose de-first and de-second! These rules make sure the door is unlocked and open before you enter the room. The others aren't necessary.

Else-if-then

With the help of else if, you can take care of alternative scenarios.

How would you greet people after 6pm? You wouldn't wish them a good day, would you?

OK computer:
if it is before 12pm
then display "Good morning!"
else if it is before 6pm
then display "Good afternoon!"
else display "Good evening!"

Fantastic! You're getting a hang of computer rule-making, aren't you?