Decision Structures

0. Introduction

Welcome to the Decision Structures Crash Course! Here's what we'll be covering:

  1. Quick review: boolean
  2. Comparison & logical operators
  3. if statements
  4. else statements
  5. else if statements
  6. Nested if statements

1. Quick review: boolean

Recall that boolean is a data type that can take on a value of either true or false. We will be using booleans to help the computer make decisions for us.

Booleans can be created by either:

We have already seen how to declare a boolean variable in previous crash courses, so let's have a look at using boolean or comparison operators to generate boolean expressions.

2. Comparison & logical operators

We can use comparison operators to compare two things (who would've thought!). Once compared, the computer tells us whether or not the comparison was true or false.
Here are some examples of boolean expressions,

We call the < , == , and >= comparison operators. As you have likely guessed, the comparison operators used in the examples were 'less than', 'equal to', and 'greater than or equal to' (if we are speaking from the left side of the operator) respectively. However, there is no need to memorize. Below is a table of all the comparison operators.

Comparison operators

# Operator English Translation
1 < less than
2 <= less than or equal to
3 > greater than
4 >= greater than or equal to
5 == equal to
6 != not equal to

These comparison operators give us basic decision making power, but sometimes we need to consider more than one condition when making decisions. For this, we have what are called logical operators. These allow us to chain boolean expressions together into mega boolean expressions for more complex decision making.

Consider the following: I want to make spaghetti, so I need to make sure I have all the ingredients. This means that in order to make spaghetti, I need pasta and sauce and meatballs and cheese.

Assuming these variables were defined previously in the code, this can be translated into the boolean expression, pasta && sauce && meatballs && cheese, where && is the logical operator 'and'. This boolean expression would have to output a value of true for me to commit to making spaghetti (i.e. each of the boolean variables must have a value of true).

Again, no memorization required. You can always refer to the table below for all the logical operators.

Logical operators

# Operator English Translation
1 && and
2 || or

If you replace all the && operators in the spaghetti example with || operators, you will need to have at least one of the ingredients listed for the boolean expression to be true. In this case, using || operators is the wrong choice since you don't want to be cleared by your program to make spaghetti only to find out that you'll be eating plain pasta for dinner. As such, it is important to be careful when deciding how to set up your boolean expressions so that the computer makes the right decision every time.

Example 1

Let's get a feel for how to use booleans practically.

  1. Create a boolean variable called i_can_code and assign it a value of true. This is the simplest way to define a booelan variable.
  2. Now, create a float variable named age and assign it the value of your age.
  3. Define another boolean variable called is_youth and set it to a boolean expression that outputs a value of true when age is less than or equal to 17. This is a better example of how we will be using boolean expressions later on.
  4. Print the value of is_youth to the console (it should print a value of true... right?).

3. If statements

The if statement is the basic building block for decision making. Here is the syntax of an if statement in its most basic form:

                    
                        if (condition_being_checked) 
                        {
                            //if the condition is true, run the code written here
                        }
                    
                

The computer interprets this quite intuitively, saying, "If the condition being checked is true, then run the code inside the curly braces."

Note how the if statement doesn't require any semi-colons (a.k.a the 'do nothing' operator). In fact, if you do add a semi-colon like this,

                    
                        if (condition_being_checked); 
                        {
                            //if the condition is true, run the code written here
                        }
                    
                

the code inside the curly braces will run regardless of if the condition being checked in the if statement was true. This is because the semi-colon terminated the if statement and then the code inside the curly braces runs just like regular code.

Example 2

Let's get the computer to tell us if we need to wear a jacket outside. Our choice should depend on the weather which can be described by the temperature and the probability of precipitation.

  1. Create two float variables named temperature and precip and assign them values of 10 and 5 respectively.
  2. Write an if statement that runs if the temperature is below 15 degrees Celsius or the probability of precipitation is higher than 30%.
  3. If the condition is satisfied, print "Take a jacket!" to the console.

4. Else statements

An else statement is optional, but can be added after an if statement if you want the computer to do something after finding that the condition being checked was false. In other words, code in the else block only runs when the condition in the if statement is false.

Here is a quick example of the use of an else statement:

            
                if (condition_being_checked) 
                {
                    //if the condition is true, run the code written here
                }
                else 
                {
                    //run this code if the condition being checked was false
                }
            
        

5. Else-if statements

Simply put, else if statements are the streamlined equivalent of putting an if statement inside an else statement. Use these when you want to check for another condition only once another condition is false. The syntax is as follows:

            
                if (1st_condition_being_checked)
                {
                    //if the 1st condition is true, run the code written here and exit the if statement
                }
                else if (2nd_condition_being_checked)
                {
                    //run this code if the 1st condition was false and the 2nd condition was true
                }
            
        

You can add as many consecutive else if statements as you wish. As with all else statements, an else statement at the end of your chain of else if statements is optional.

6. Nested if statements

The practice of "nesting" consists of putting one thing inside another. A nested if statement, then, is simply putting an if statement inside another. Nested if statements are basically the opposite of else if statements. The code inside an else if block only runs if the condition in the preceding if statement was false, and the code inside a nested if statement only runs if the condition in the preceding if statement was true.

You can probably picture it already, but here is a sample of a nested if statement.

            
                if (condition) 
                {
                    //the 2nd if statement is only considered if the 1st condition is true

                    if(2nd_condition)
                    {
                        //run this code if the 2nd condition is true
                    }
                }
            
        

Congratulations! You've completed the Decision Structures Crash Course. Move on to 4. Repetition Structures to keep learning (there are 4 parts!).