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.
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open the ProjectStart MPLAB® X, then click on the Open Project 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 .
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”.
Once you finish writing the code:Click on the Debug Project button. This will build and send the program to the simulator.
Click on the Continue button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt button. This will stop execution so that we may examine the variables and their values.
End Debug SessionEnd the Simulation Session by clicking the Finish Debugger Session 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.