Lab Exercise 6: switch Statements

Last modified by Microchip on 2023/11/09 09:21

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 conditional expressions.

Reference Materials

Exercise Files

Back to top

Procedure

Open the Project

Start MPLAB® X IDE, then click on the Open Project Open project icon ​​​​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 Open project button.


Open C Source File

​ Open C Source File

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

This brings up Lab06.c in a window to edit.


Edit Source File

The task for this lab is to create a switch statement that prints out a particular string depending on the value of the control variable. Like some of the examples in the presentation, Chicago TV channels and their American network affiliations are used as 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 increments 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 channelchannel is our control variable, which increments from 1 to 10 in the main loop. During each pass, you use the switch statement to print out the appropriate string based on the value of the channel.

Write the case for channel = CBS (CBS is a constant defined to equal 2). There are two things that need to be done here. First, you need to start a case block, and then within the block, you 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 you changed the constant at the top of the file to be 9? You would correctly get to this point when channel = 9, but that 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 the 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".

Switch statement diagram


Debug Project

Once you finish writing the code:

Click on the Debug Project Debug icon ​​​​​​button. This builds and sends the program to the simulator.
Click on the Continue Continue icon button. This begins the simulation. Wait for the UART 1 Output window to finish printing.
Click on the Halt Pause icon button. This stops execution so that you may examine the variables and their values.

Back to top


Results

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

Results are shown in the UART1 Output

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Finish debug icon button.

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

Close the Project.

Back to top

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 the 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 is 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, you 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 is executed only when channel = CBS. Use a compound statement (enclosed by curly braces) so that there can be multiple instructions as part of this case block. The printf function makes use of the channel variable so that if you change the value of the constant CBS above, this correctly prints out the new channel number associated with this network. Finally, use a break statement to force us out of the switch block since the current value of channel is already handled. If you eliminated the break statement, you would fall through to the next case and execute it, and so on until the end of the switch block is hit.

Lines 81-87
STEP 3:
Step 3 looks just like step 2, but here you 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 you 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, you simply want to print out "- - - #" where # is the current channel number. Unlike the cases above, a break statement is not needed here since there is nowhere left to fall through — you are already at the end.

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

Back to top

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.

Back to top