Lab Exercise 6: switch Statements


The purpose of this lab is to illustrate the use of the switch statement to make decisions in code. A switch statement will execute 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 how you create the condition expressions.

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



Open the Project

Start MPLAB® X, then click on the Open Project Main_Open_Project.png icon on 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.


Open C Source File


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


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).

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 channel.

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 will work in this circumstance, while 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.

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.

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.

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”.



Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send 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 will stop execution so that we may examine the variables and their values.



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.


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.

© 2017 Microchip Technology, Inc.
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.