Conditions: A Computational Way to Make Decisions

We often want to be able to do things in our programs “conditionally.” We want to be able to say “if this thing is true, then do X, but if this other thing is true, then do Y.”

The if-then statement allows the program to branch off and execute one of two different blocks of code. The if-statement starts by evaluating a Boolean clause. If this clause evaluates to be TRUE, the block of code conditioned on this if-statement is executed. If an else-statement is present, it can provide another block of code to be executed if the statement evaluated is FALSE. That’s Boolean logic in simple terms - a continual evaluation of TRUE and FALSE.

If you stop to think about it, then you’d see that we do things conditionally every day. It’s spring here in north Texas, and we have crazy and unpredictable weather. When I wake up in the morning, I must check the weather if I'm going to be prepared. If it’s raining outside, then I take an umbrella, else I wear sunglasses.

Creative Commons Attribution 2.0 Generic · Photograph of the Penn State college campus in the rain

Creative Commons Attribution 2.0 Generic · Photograph of the Penn State college campus in the rain

Asking questions like a computer

As humans, we often make decisions based on a complex set of conditions, including the circumstances, our own preferences, past experience and even how we feel. When we write a program, we need to represent our decisions in ways a computer will understand.

We know now that the computer evaluates some statement (also called an expression) that can only be TRUE or FALSE. There is no assessment of a condition that evaluates “Banana,” for example. The result will determine which block of code the program will evaluate next.

The types of statements that a computer can evaluate as either TRUE or FALSE is also limited by the fact that information is stored in the computer as binary. As a result, most TRUE and FALSE statements you will use in your programs are comparing two values in the computer’s memory.

A good way to check if an expression evaluates to a Boolean is to stick the word ‘is’ in front of it and ask it a question. If it sounds like a yes or no question, then you know it’s a Boolean expression. Here are the most common types of comparisons:

____ is equal to _____

____ is not equal to _____

____ is greater than ____

____ is less than _____

____ is greater than or equal to ____

____ is less than or equal to____

It can take a little practice to convert a question you might ask as a human into a binary statement evaluated by a computer. Here are some examples:

Human question:  Is it lunch time yet?
Computer question: Is it 12:00 p.m.?

Human question: Is she old enough to drive?
Computer question: Is her age equal to or greater than 16?

Human question: Should we see this movie?
Computer questions: Is the number of seats left in the theater equal to or greater than 12? Is the number of stars for the movie greater than two? Is the movie genre equal to comedy?

Want to give it a try?

You can do this! Write a program that answers our last question above: What movie should we see?

Photo credit: Alamo Drafthouse Cinema

Photo credit: Alamo Drafthouse Cinema

Here’s the scenario: Our team, including our mentors, are going to the movies. We know what movies are showing, but how do we decide which movie to see? We want to see a movie we think everyone will enjoy, and we want to make sure there are enough seats available. We can make this decision easier if we answer some questions about each movie:

  • How many stars did the critics give it? If the movie is less than 3 stars, we don’t want to see it
  • What genre is it? Let’s pretend our favorite genre is super-hero movies. If the movie is our favorite genre, we’ll go see it no matter what the ratings are.
  • How many seats are available in the theater? If there aren’t enough seats for all of us, we don’t want to see it.

With a partner, create a program that help us answer these questions using Boolean expressions.

Get started:

  1. Visit, and copy the code into your own PencilCode account. To do this you click on the down-arrow next to “Save”, and click “Copy and Save As”.
  2. In the code you see an object, the movies object, and it holds all the movies that are showing this week. You will learn about objects more later. Under the object, you see a for loop with a block, which will display all of the movies in the movies object.
  3. To refer to the information in the objects, you only need to type the name of the object, period, and then the part inside the object. For example: If I want the name of the movie I type in or if I want the stars I type in movie.stars.
  4. Remember to use conditional statements and relational operators in order to get the output you want. First you might want to write out what type of movies you are listing first, for example superhero movies.
  5. Within the loop, add conditional if-statements to only write out the movies that match our criteria. You might need to nest one if statement inside another. You can also combine Boolean expressions into one line using “and”.

CHALLENGE: Try adding your own movies to the list. In text mode you can copy-and-paste a movie on a new line and change the variables.

Making decisions based upon information is an important part of what makes computers seem intelligent! With some practice, you can also get used to writing conditions that your programs can evaluate and execute.