Finishing off your decision, part 1
Using else by itself with if decisions
This topic will almost complete the package of tools that can be used in branching processes. So far, all of the branching or decision making conditions you have worked with have been optional. In other words, there were certain circumstances where, because of the configuration of the logical conditions, all the tests could occur without any decision being made.
However, there always comes a time where you want to, or have to, force the program to branch in a certain direction if none of the others will work. This is commonly called "accepting the default condition", and in fact, one of the conditions in the switch statement (next topic) actually uses the default keyword. Once you have this part wrapped up, you can create much more significant programs, and continue to move on to further Computer Science learning.
Using else with the if Statement
In the previous sections, your branching process was an option. In other words, there were some conditions that supported changing the path of the program, and some that did not. It was possible to implement even a very long if..else statement, and still not make any choice at all. For example, in the case of the ticket price condition, if someone wanted to come to the amusement park who was older than 99, the ticket sellers would not know what to charge the person.
Sometimes, as in the amusement park example, you must have some kind of default condition that will force some action if the decision making process does not cover all the possibilities. A default choice or condition is one that happens if no choices are made, or sometimes if no other choices are available. It can also be a choice that is made if none of the other choices are met by the program. Here is an example of the pseudo code for the ticket exercise again.
// check for person younger than 13
// set ticket price to $6.00
// otherwise, check for person younger than 19
// set ticket price to $9.00
// otherwise, check for person younger than 26
// set ticket price to $12.00
// otherwise, check for person younger than 55
// set ticket price to $15.00
// otherwise, assume person >= 55
// set ticket price to $10.00
The program code for this condition is easy. You just use the keyword else without using another if statement. Consider the modified code next.
// get age from user
age = GetAge();
// check for age < 13
if( age < 13 )
{
// set ticket price to $6.00
ticketPrice = 6.00;
}
// otherwise, check for age < 19
else if( age < 19 )
{
// set ticket price to $9.00
ticketPrice = 9.00;
}
// otherwise, check for age < 26
else if( age < 26 )
{
// set ticket price to $12.00
ticketPrice = 12.00;
}
// otherwise, check for age < 55
else if( age < 55 ) )
{
// set ticket price to $15.00
ticketPrice = 15.00;
}
// otherwise, assume age >= 55
else // else keyword used to provide default
{
// set ticket price to $10.00
ticketPrice = 10.00;
}
Consider that you could have implemented one more if test. For this situation, you might have used the following code.
//
// other if & else..if decisions
//
else if( age >= 55 )
{
ticketPrice = 10.00;
}
The question to ask is why? Since you have tested for all ages below 55 by this point in your code, you can trust that only ages 55 and older are going to make it to this point in the code. This again is a simple application of logic: If all ages below 55 have been tested and have not been found true, the only ages possible are 55 and above. Thinking your way through logical conditions such as this one will allow you to write cleaner and more efficient code, and make your programs more safe and robust.
The else keyword makes a couple of changes to the use of the if statement. First, it provides an alternative to the first if. The process is telling the computer, if you do not follow this path, you must follow this other path. Unlike the original if statements you have studied previously, when an else is used, there will be some decision made. The if statement in the previous topics would either take some action if the logical condition were true, or it would not take an action, leaving the program to run without change.
As an analogy, consider driving into a normal four way intersection. You can decide to turn left, or to turn right, or you can just decide to continue on the way you were going. However, if you get to a 'Y' intersection, or a 'T' intersection, you must choose one path (i.e., branch) or the other. You cannot continue on the path on which you were previously headed.
The good news about the if..else combination is that it is intuitive (i.e., it is natural and makes sense to you). You can think the process through in your mind as you consider how to create a decision making process. If there is a condition that requires an "either - or" choice, you will use the if..else combination.
For example, back at the amusement park, there are rides that require a minimum height. Of course, there are children's rides that would be too small for people over a certain height. If your amusement park ride required that you were at least one meter tall to ride on the big roller coaster, but the small roller coaster does not allow people over one meter, your decision making process would look like the following.
// check for height > 1 meter
if( height > 1 )
{
// set big roller coaster flag to true,
// small roller coaster flag to false
ridesBigRollerCoaster = true;
ridesSmallRollerCoaster = false;
}
// otherwise, if height not > 1 meter
else
{
// set big roller coaster flag to false,
// small roller coaster flag to true
ridesBigRollerCoaster = false;
ridesSmallRollerCoaster = true;
}
Once again, this is an either/or condition. When people come into the amusement park, they might get a green stamp on their hand for large roller coasters, or a blue stamp on their hand to ride the small roller coaster. It is important to note that no one gets in without a stamp; in other words, one of the two actions must be implemented. There is no "no decision" condition as there was with only the if statement.
Consider the following graphical presentation of the code progress.
Program steps: 1) Program branch if( height > 1 ), then 2) else, then 3) Next Program Step
As you can see, the step-wise process of the program goes into the if..else test, makes its choice, and then continues on. Below is a graphic of the branching process.
Once more, remember that there is no test at all for the else condition; the else or default choice is made simply because no other choices met the criteria. Logically, if the if statement tests for "height greater than one", then the else statement implicitly catches the "height less than or equal to one". This can also be stated as "height not greater than one". This is simple logic. The decision is between "greater than one" and "not greater than one". Simple as it sounds, the ability to recognize this condition will be very important to you as you continue with your computer science studies.
There is not much more to discuss with the else keyword; however, there are sometimes some mistakes made with it. Watch this video so you can see how to make sure you are getting this right.