Section 6k

Selecting from Multiple Options - switch


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A simple decision making tool

As your knowledge of branching grows, your ability to create flexible programs is growing even more. There are many things you will be able to do with programs when you have completed these topics. For now, you will be looking at the switch statement which supports larger scale branching in an easier way. Once you have worked with this, you will be able to look at "forced" choices, and you will have covered the material.

Using the switch statement

Consider the branching process where there are several possible outcomes based on one value. For example, consider the following pseudo code conditions.

If the employee is 55, he or she may take early retirement with some benefit from the company, but no benefit from Social Security

If the employee is 62, he or she may take early retirement with full benefit from the company, and some benefit from Social Security

If the employee is 65, he or she may retire with full benefits from the company, and full benefits from Social Security

First, you know that it is possible to put this into a set of if statements as shown here.

// check for employee age 55
if( employeeAge == 55 )
{
// display partial company retirement message, no social security
printString( "Partial Company retirement, no Social Security" );
printEndline();
}

// check for employee age 62
if( employeeAge == 62 )
{
// display full company retirement message, limited social security
printString( "Full Company retirement, limited Social Security" );
printEndline();
}

// check for employee age 65 if( employeeAge == 65 )
{
// display full company retirement, full social security message
printString( "Full Company retirement, full Social Security" );
printEndline();
}

Notice that these are specific age values. They are not ranges as you found in the amusement park ticket analysis earlier. The problem is, this kind of if test could have other possibilities, and you could end up writing a whole list of if statements that say almost the same thing - only the age value is changed.

The if and if..else statements, and their various combinations, will serve you well for most of your branching conditions. However, the C programming language, as well as many others, does offer another branching tool that can be handy if there are several action choices from one branching condition. The general form of this code is shown here.

switch ( < character or integer variable > )
{
case < character or integer value >:
[ < action taken > ]
[ < action taken > ]
[ break; ]
.

.

.
case < character or integer value >:
[ < action taken > ]
[ < action taken > ]
[ break; ]
.

.
}

For your retirement age issue above, the switch statement would be implemented as shown here.

// select result using employee age
switch( employeeAge )
{
// check for case 55
case 55:
// display partial company retirement message
printString( "Partial Company retirement, no Social Security" );
printEndline();
break;

// check for case 62
case 62:
// display full company retirement message
printString( "Full Company retirement, limited Social Security" );
printEndline();
break;

// check for case 65
case 65:
// display full company retirement
// and full social security message
printString( "Full Company retirement, full Social Security" );
printEndline();
break;
}

There is no limit to the number of action conditions implemented from this branching operation. Here is a simple example with the days in a month. Note that the switch statement can only use integers and characters, which are a special case of the integer data type. Floating point values will not work for this testing process.

// declare necessary variables
int daysInMonth, monthValue;

// gets a month value between 1 and 12
monthValue = GetMonthValue();

// select result based on month value
switch( monthValue )
{
// case - January
case 1:
// set days in month to 31
daysInMonth = 31;
break;

// case - February, assumes no leap year
case 2:
// set days in month to 28
daysInMonth = 28;
break;

// case - March
case 3:
// set days in month to 31
daysInMonth = 31;
break;

// case - April
case 4:
// set days in month to 30
daysInMonth = 30;
break;

// case - May
case 5:
// set days in month to 31
daysInMonth = 31;
break;

// case - June
case 6:
// set days in month to 30
daysInMonth = 30;
break;

// case - July
case 7:
// set days in month to 31
daysInMonth = 31;
break;

// case - August
case 8:
// set days in month to 31
daysInMonth = 31;
break;

// case - September
case 9:
// set days in month to 30
daysInMonth = 30;
break;

// case - October
case 10:
// set days in month to 31
daysInMonth = 31;
break;

// case - November
case 11:
// set days in month to 30
daysInMonth = 30;
break;

// case - December
case 12:
// set days in month to 31
daysInMonth = 31;
break;
}

Having looked at this, there are a couple of ways you can make this decision-making process even easier ... and in the case of leap year, more correct. Consider the following.

// declare necessary variables
int daysInMonth, monthValue, yearValue;

// gets a month value between 1 and 12
monthValue = GetMonthValue();
yearValue = getYearValue();

// select result based on month value
switch( monthValue )
{
// case - 31 day months
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
// set days in month to 31
daysInMonth = 31;
break;

// case - 30 day months
case 4: // April
case 6: // June
case 9: // September
case 11: // November
// set days in month to 30
daysInMonth = 30;
break;

// case - February
case 2:
// check for leap year
// function: isLeapYear
if( isLeapYear( yearValue ) )
{
// set days in month to 29
daysInMonth = 29;
}

// otherwise, if not leap year
else if( !isLeapYear( yearValue ) )
{
// set days in month to 28
daysInMonth = 28;
}
break;
}

The above example shows that if more than one value evaluates to the same results, you can simply use multiple case statements. Also note that you can write other code inside switch statements, such as the if statement used for leap year, but be careful not to make the code too complicated. If the code gets too long or complicated, you should break parts of it out into methods.

The use of the switch statement also introduces you to the break keyword. In a switch statement, once you have implemented the actions under a choice (i.e., the case keyword), the switch statement would let the program steps continue on through the next code steps all the way to the end if you did not stop it. The break keyword tells the program to go to the end of the present switch statement (i.e., the closing curly brace) and then continue with the next steps of the program. Generally speaking, you will implement most of your switch statements with break statements after the completion of the selected code block; however, there are conditions -- although uncommon -- where you might leave it off, so it is an option.

Watch this video to see how switch statements can be used to replace if statements, and then with a little logical thinking, can be refined even further.

As a side note to the above code, you had to use the isLeapYear function twice in the code, once for when the year was leap year and once for when it was not. The logical result of testing for leap year is that it either is a leap year, or it isn't. In the case of the code above, the test function had to be called twice. This is redundant and is not a good practice (although necessary for this educational situation). In the next section, you will see how to use logic so you would only have to use the function once. Stay Tuned.

Increasingly Complex Tests

You are gaining knowledge over how to manage your computer decision making processes. There are still more things to learn but they come at the price of increased complexity. Make sure you understand what is happening in these processes before you go on.