Lab Exercise 6: switch Statements

 Objective

The purpose of this lab is to illustrate the use of the switch statement to make decisions in code. A switch statement executes one (or more) blocks of code depending on which condition is met. The goal of this lab is for you to become comfortable with the switch statement syntax and to teach you to create the conditional expressions.

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

1

Open the Project

Start MPLAB® X IDE, then click on the Open Project Main_Open_Project.png icon in the main toolbar.

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab06.X folder.

Select Open Project OpenProjectButton.png.

2

Open C Source File

Lab6C.png

Open the Lab06.c source file from the project tree by double clicking on its icon.

This will bring up Lab06.c in a window to edit.

3

Edit Source File

The task for this lab is to create a switch statement that will print out a particular string depending on the value of the control variable. Like some of the examples in the presentation, we will use Chicago TV channels and their American network affiliations as our data. (Feel free to localize the code to print out “CBC”, “BBC”, “Telemundo” or whatever you like.) Note that some constants have been defined to equate the network’s initials with the TV channel number (CBS = 2, NBC = 5, ABC = 7).

The main loop of the program will increment the channel variable from 1 to 10. Our task is to print out either the network initials with its associated channel or three dashes followed by the channel if there is no network affiliation. (See flow chart below).

STEP 1:
Open a switch statement on the variable channel. channel is our control variable, which will be incremented from 1 to 10 in the main loop. During each pass, we will use the switch statement to print out the appropriate string based on the value of the channel.

STEP 2:
Write case for channel = CBS (CBS is a constant defined to equal 2). There are two things that need to be done here. First, we need to start a case block, and then within the block, we need to print out the string CBS 2. There are two ways to do this. One would be to simply do print(“CBS 2\n”). While this works in this circumstance and if the constant CBS is defined to be 2, what would happen if we changed the constant at the top of the file to be 9? We would correctly get to this point when channel = 9, but we would incorrectly print out “CBS 2”. So the better way to code this is to use the channel variable in our print statement. Remember the syntax for printf:

printf(ControlString, arg1, arg2, ... , argN)

You can use %d as the placeholder in your string for the channel variable which would be the only argument used.

STEP 3:
Write the case for channel = NBC (NBC is a constant defined to equal 5). This step should look identical to step 2, but with the appropriate values used for NBC.

STEP 4:
Write the case for channel = ABC (ABC is a constant defined to equal 7). This step should look identical to step 2, but with the appropriate values used for ABC.

STEP 5:
Write the default case. If channel is anything other than those listed above, this is what should be done. For these cases, you need to print the string "- - - #" where # is the channel number (value of the channel variable).
For example, if channel = 6, you should print "- - - 6".

diagram.png

4

Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This builds and sends the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt Debug_Pause.png button. This stops execution so that we may examine the variables and their values.

 Results

After you build the code, run it and then stop it, you should see the following text in the UART1 Output window:

Lab06.png

5

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png button.

Clear out the UART 1 Output window (Ctrl + L).

Close the Project.

 Analysis

Line numbers correspond to those in the provided solution file.

Lines 59-60
STEP 1:
Step 1 asks you to open up a switch statement on the variable channel. Basically, all you need to do is write the first line(s) of code that are required to start off a switch statement with channel as the control variable:

switch(channel)
{

This is just following the syntax definition for a switch statement. The variable channel is the one that will be evaluated for each of the case blocks that follow below.

Lines 67-73
STEP 2:
Write the case for channel = CBS (using the predefined constants from above). The first part of this step is to open up the case block with the case keyword and the value that is to be matched with the channel variable. Next, within the block, we need to print out "CBS 2" using the printf function, and finally finish up the block with a break statement to prevent fall through to the next case.

case CBS:
{
printf("CBS %d\n", channel);
break;
}

The first line makes it so that this block will only be executed when channel = CBS. We then use a compound statement (enclosed by curly braces) so that we can have multiple instructions as part of this case block. The printf function makes use of the channel variable so that if we change the value of the constant CBS above, this will correctly print out the new channel number associated with this network. Finally, we use a break statement to force us out of the switch block since we have already handled the current value of channel. If we eliminated the break statement, we would fall through to the next case and execute it, and so on until we hit the end of the switch block.

Lines 81-87
STEP 3:
Step 3 looks just like step 2, but here we must substitute the appropriate text and values for NBC.

case NBC:
{
printf("NBC %d\n", channel);
break;
}

Lines 95-101
STEP 4:
Step 4 looks just like step 2, but here we must substitute the appropriate text and values for ABC.

case ABC:
{
printf("ABC %d\n", channel);
break;
}

Lines 111-115
STEP 5:
Step 5 completes the switch statement with the default case. If none of the above cases are true, this is the one that gets executed. Here, we simply want to print out "- - - #" where # is the current channel number. Unlike the cases above, we don’t need a break statement here since there is nowhere left to fall through — we are already at the end.

default:
{
printf("—- %d\n", channel);
}

 Conclusions

The switch statement provides a more elegant way to conditionally execute blocks of code based on multiple criteria than the if statement. The only limitation is that the case conditions must be constant, or some value that may be evaluated at compile time, whereas the if statement allows variables to be used as part of its conditions.

© 2019 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.