Lesson 9.2: Conditional Statements – Branching Conditionals
In the previous part of this lesson we looked briefly at what a conditional statement is in simple terms. In this portion, we’ll take a look at the simpler types of conditional statements, the branching conditionals.
What are Branching Conditionals?
As we stated in the last portion, branching conditionals are conditional statements that have “branches” (though sometimes it’s only one branch), and performs only the actions in on branch based on the condition. The common type of branching conditional statements are if, if…else, and switch.
If Statements
If statements are statements that take a condition, and perform the action if the condition evaluates to true. You can remember this by making it a simple sentence:
“If something is true then do this action.”
In pseudo-code, we would write a condition like this:
If bob = true Then
Print "It's Bob!"
End If
This is only pseudo-code, but gives the basic idea. “bob == true” is the condition we are checking. The action we perform is to print out “It’s Bob!”. We then have an End If signifying that we are done with this branch. We can have multiple statements in the If statement. We could also have multiple actions within the If, which is why we have the End If. For example, here is something that prints three messages:
If bob = true Then
Print "It's Bob!"
Print "Hello Bob!"
Print "Bye Bob!"
End If
We can also have compound conditions within our condition as well. For example:
If bob = true AND bob_is_here = true Then
Print "Bob is finally here!"
End If
So, if you follow your Boolean arithmetic we would get the following results based on our variables:
| bob | bob_is_here | Result |
| true | true | Prints |
| true | false | Nothing |
| false | true | Nothing |
| false | false | Nothing |
You can create as crazy a conditional as you want within the If statement (or any other conditional statement). It will still boil it all down to either true or false and take the action if it is true.
Now, I said that the If statement is a branching condition, but it only has one branch. To give it multiple branches we’ll need to add in the Else statement.
If…Else Statements
The Else statement let’s you perform an action if the previous If statement was false. To add on to our simple sentence from earlier:
“If something is true then do this action, else do this other action.”
Here is an example If…Else:
If bob = true Then
Print "Bob is here!"
Else
Print "Bob is gone!"
End If
Now, if bob is true, it will print “Bob is here!”. Unlike last time, if bob is not true (so bob is false), it will print “Bob is gone!”, instead of just doing nothing. Now, we have two branches in our statement.
We can also chain together If…Else statements, which allow us to add as many branches as we want. For example:
If bob_is_here = true Then
Print "Bob is here!"
Else If tom_is_here = true Then
Print "Tom is here!"
Else
Print "Nobody is here!"
Now, this can be a bit tricky. Just remember that it will only perform one of these branches, and it will perform the first one it finds is true. So, here is our table of different values for our variables and what our result is:
| bob_is_here | tom_is_here | Result |
| true | true | “Bob is here!” |
| true | false | “Bob is here!” |
| false | true | “Tom is here!” |
| false | false | “Nobody is here!” |
Notice how if bob_is_here is true, regardless of what tom_is_here is, the result is always “Bob is here!” because it’s first. If bob_is_here is false, then we might say “Tom is here!” (if he is), or we will say “Nobody is here!” if our other statements don’t evaluate true.
Keep in mind that our examples have an Else at the end to catch it if we don’t do anything, but we don’t have to have one. We can just have an If and Else If, without a final Else if we don’t want it to do anything if both aren’t true.
Switch Statements
Our final common branching conditional is the Switch statement. Switches are essentially the same in function as If…Else If…Else statements, but differ in form.
A Switch statement takes a value (not just a Boolean), and then compares it’s value to a number of Cases which you then define. Some languages only allow you to use integer (whole number) values, while others allow you to use just about any type. For our pseudo-code, we’ll assume we can only use integers.
Switch myValue
Case 0:
Print "Your value is zero."
End Case
Case 1:
Print "Your value is one."
End Case
Default Case:
Print "Your value isn't one or zero."
End Case
End Switch
In this example, if the value of myValue is 0, it will print “Your value is zero.” If the value is 1, it’ll print “Your value is one.” If it is neither of these, it will use the Default case and print “Your value isn’t one or zero.” The Default case is basically the same as an Else. You can write this same switch statement as an If…Else If…Else statement as well:
If myValue = 0 Then Print "Your value is zero." Else If myValue = 1 Then Print "Your value is one." Else Print "Your value isn't one or zero." End If
The beauty of the switch statement doesn’t really shine when you have only a few cases, but if you have a larger number, then the switch because much easier to write then a large compound If…Else If…End If statement.
Nesting
The final topic we’ll cover in this lesson is nesting. Nesting is the term used when you put one (or more) conditional statements inside of another (you create nested conditional statements).
There isn’t really anything surprising about nesting branching statements. Each conditional statement will still perform it’s own block, and each nesting will perform it’s own. For each level you would have one branch performed. Here is an example:
If bob = true Then
If bob_is_here = true Then
Print "Bob is here!"
Else
Print "It's Bob, but he isn't here!"
End If
Else If tom = true Then
If tom_is_here = true Then
Print "Tom is here!"
Else
Print "It's Tom, but he isn't here!"
End If
Else
Print "It's nobody!"
End If
Now, in this case our first level has three branches (If bob = true, If tom = true, Else). The first two branches then have a nested If statement, which also contain three branches. Before you take a look at the table below, see predict what you think would be the result for each table and see if you get it right. You can also have as many nested levels as you want, and mix branching and looping conditionals, or anything else for that matter.
Here are the results of our last example, depending on our variables:
| bob | tom | bob_is_here | tom_is_here | Result |
| true | true | true | true | “Bob is here!” |
| true | true | true | false | “Bob is here!” |
| true | true | false | true | “Bob is here!” |
| true | true | false | false | “Bob is here!” |
| true | false | true | true | “It’s Bob, but he isn’t here!” |
| true | false | true | false | “It’s Bob, but he isn’t here!” |
| true | false | false | true | “It’s Bob, but he isn’t here!” |
| true | false | false | false | “It’s Bob, but he isn’t here!” |
| false | true | true | true | “Tom is here!” |
| false | true | true | false | “It’s Tom, but he isn’t here!” |
| false | true | false | true | “It’s nobody!” |
| false | true | false | false | “It’s nobody!” |
| false | false | true | true | “Tom is here!” |
| false | false | true | false | “It’s Tom, but he’s not here!” |
| false | false | false | true | “It’s nobody!” |
| false | false | false | false | “It’s nobody!” |
In this lesson we discussed the common types of Branching Conditional Statements. In our next lesson we’ll talk about the other type of conditional statements: loops.





November 9th, 2009 at 2:15 pm