Section 6h

Selecting from Multiple Options - if


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Extending the Power of if

Choices, Choices, Choices

Now that you have been introduced to the if statement, it is easy to expand on it. There are essentially two more tricks to using the if statement, but only one of them will be addressed in this topic; the other will be presented later. In this topic, you will see that you can make more than one choice with the same data, and you will see the value of logically processing the information with which you must work.

Using if . . else

Once you have the basics of the if statement down, you can move into more complexity in your program decision making processes. Of course, more complexity is not necessarily better, but it is sometimes necessary. Before you get into combining conditions, however, there is another way to implement a decision. It is the test condition else . . if.

The else . . if test allows you to make a chain or series of separate, but related decisions. Consider an amusement park example. Suppose your ticket price to an amusement park is based on age. The prices might be specified as follows:

Consider the following code example using only the if test condition.

age = GetAge();

if( age < 13 )
{
ticketPrice = 6.00;
}

if( ( age > 12 ) && ( age < 19 ) )
{
ticketPrice = 9.00;
}

if( ( age >= 19 ) && ( age <= 25 ) )
{
ticketPrice = 12.00;
}

if( ( age >= 26 ) && ( age <= 54 ) )
{
ticketPrice = 15.00;
}

if ( ( age >= 55 ) && ( age < 100 ) )
{
ticketPrice = 10.00;
}

There are a couple of comments to make about the above example. First, there are easier ways to implement this testing process which you will be reading about soon. Next, notice the use of the AND operator as part of the conditional test. For the college students, the requirement was for them to be older than or equal to 19 years old, AND less than or equal to 25 years old. If you use this kind of strategy, you must bracket ages like this with an AND.

The last thing to notice about the above code example is that there are two kinds of "age bracketing" tests. One is a test that uses a combination of less than or equal operators ("<="), and greater than or equal operators (">="), and the other simply uses less than ("<") or greater than (">") operators. The example of testing for the teenager ages was implemented as shown below.

if( ( age > 12 ) && ( age < 19 ) )
{
ticketPrice = 9.00;
}

However, it could have been implemented as shown here.

if( ( age >= 13 ) && ( age <= 18 ) )
{
ticketPrice = 9.00;
}

Either form of the test is legitimate and appropriate. In older days when combined operators cost a little more processing time, some programmers tried to use only the simple operators instead of the combinations. However, the difference in processing is negligible with the power of present day computers. Therefore, either form is okay as long as you understand the ages that are to meet the test and the ages that are not.

The above example is a perfectly legal way to test for the correct age conditions, but it is very inefficient. The computer program will have to make five different tests before it goes on to the next action. The graphic below shows the branching conditions for this testing process.

Graphical representation of multiple if tests

As you can see here, there are five different tests implemented, but it is still possible for no decision to be made throughout all five tests. Now consider the following code segment.

age = GetAge();

if( age < 13 )
{
ticketPrice = 6.00; }

else if( ( age > 12 ) && ( age < 19 ) )
{
ticketPrice = 9.00;
}

else if( ( age >= 19 ) && ( age <= 25 ) )
{
ticketPrice = 12.00;
}

else if( ( age >= 26 ) && ( age <= 54 ) )
{
ticketPrice = 15.00;
}

else if ( age < 100 )
{
ticketPrice = 10.00;
}

The difference is that for all but the first condition, the keyword else has been placed in front of the remaining if statements. However, while the conditional test results will be the same, the testing process will be significantly different. The branching condition for the if..else..if action is shown below.

Graphical representation of multiple if-else-if tests

The graphic shows that the whole testing process is implemented in steps as before. However, if one of the conditions evaluates to true, the remainder of the tests are skipped. Again, in the condition shown earlier, several if statements in a row would have to be evaluated. With the if..else..if condition, once a decision is made, there are no more tests. You will see some further value to this in a later topic.

The problems with decision-making without the else keyword can be taken one step further if you will apply some basic logic. Consider the test for teenagers (i.e., between ages 12 and 19). Consider the fact that when the program gets to the point of testing for this age bracket, it has already tested for less than age 13. If you think about it, that means you already know that since the program did not accept the choice of less than 13, the person must be 13 or older. This means that you really should not have to test for the lower end of the next age. Consider the following pseudo code.

// check for person younger than 13

// set ticket price to $6.00

// check for person younger than 19

// set ticket price to $9.00

// check for person younger than 26

// set ticket price to $12.00

// check for person younger than 55

// set ticket price to $15.00

// check for person younger than 100

// set ticket price to $10.00

Now spend a moment thinking about the above logical statements. If these statements were implemented as program code, the program would function as follows:

In fact, if the program code went all the way through the tests above, every person would be charged $10.00 (i.e., the last of the choices) no matter what his or her age . . . at least everyone under 100. This is one of those conditions where you really wish the computer would not do what you told it to do. Unfortunately, computers do not know any better.

The next example uses simpler decision making statements, and gets the job done correctly, using the else keyword. Consider the pseudo code, modified from above by adding the "otherwise" pseudocode term -- representing the else keyword -- to the remainder of the statements

// 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, check for person younger than 100

// set ticket price to $10.00

Now the computer operations will look like the following.

Now, since the computer has been given else keywords in front of the remaining if statements, it will stop making any tests after one test succeeds. The code would appear as shown here.

age = GetAge();

// check for person younger than 13
if( age < 13 )
{
// set ticket price to $6.00
ticketPrice = 6.00;
}

// otherwise, check for person younger than 19
else if( age < 19 )
{
// set ticket price to $9.00
ticketPrice = 9.00;
}

// otherwise, check for person younger than 26
else if( age < 26 )
{
// set ticket price to $12.00
ticketPrice = 12.00;
}

// otherwise, check for person younger than 55
else if( age < 55 ) )
{
// set ticket price to $15.00
ticketPrice = 15.00;
}

// otherwise, check for person younger than 100
else if ( age < 100 )
{
// set ticket price to $10.00
ticketPrice = 10.00;
}

You have implemented two improvements to your code here. First, once a test is successful, no other tests will be conducted. Since you know that, you can accomplish the second thing, which is to make your logic simpler. If you have already tested for less than 13, and the test failed, then the test for less than 19 will only be true if the ticket purchaser is between the ages 12 and 19. Simpler code, better programming, less confusing, less apt to be messed up - all in one package.

To be clear, the C programming language doesn’t actually have an else . . if operation. Instead it has if and else. That said, you can ask an if question, then if that is not true, you can move to an else state. However, once you get there, you can ask another if question if you wish to. The use of the else operation by itself will be discussed later.

Now as a final statement for this short section, remember that your decision making process still has not been forced. It is actually possible that someone over 100 years old wants to come to your amusement park. So, it is still possible that your program will make "no decision", meaning, if the person was 102, none of the if..else..if statements would cover that condition. In a later section, you will be looking at how to "force" a decision for conditions like this one.

Watch this video to see a summation of the work shown on this page. This includes some design statement work as well as thinking about the logic of multiple if statements.