Switch is a statement of Control Flow that replaces multiples if‘s and merges into one elegant decision making solution. When the program has to choose between many branches and options in a program, Switch becomes handy to use since it has a ‘default' option if none of the options and conditions are met. Keep reading to know how to use it.
Table of Contents
Sample of Switch in C
Let's see a typical sample of a Switch statement. In the code below, the statement checks the value of the variable ‘number‘ to see how to proceed.
- if the value is 1, then it executes the code within case(1) which increases the value of the variable times by 1.
- if the value is 2, then it executes the code within case(2) which increases the value of the variable times by 2.
- if the value is not listed, then it will execute the default case which restarts the variable times.
switch(number){ //Selection by the number case(1): times=times+1; break; case(2): times=times+2; break; case(3): times=times+3; break; default: times=0; }
Syntax of Switch
So, how does Switch in C programming language looks like?
switch(expression){ //Selection by the number case(1): //in case that expression is 1, do this break; case(2): //in case that expression is 2, do this break; default: //if no other option for expression is has met, do this //no 'break' here }
Switch
It begins with Switch and then the expression which the whole statement is based. Next comes the expression (where ‘number' is located) to indicate the rule or condition for each case.
Case
Next, as you guessed, comes the cases. For every possibility that expression can result in, the inside that case will be executed; however, if no case is found for the criteria, then a default code will be executed, which is located at the end of the function.
Break
This statement requires to be at the end of the case, in order for the program to quit from such case.
If break is not placed, then it will jump into the next case automatically, as shown in this chart:
Example of Switch in C
Example 1
In this post, a microcontroller PIC16F684 drives a 7 segment LED display. Every second, the number in the display is increased by one. The switch function decides which number to display by asking the variable sequence and then turns the corresponding LEDs. For example, if it is the number 7 in the sequence, then Switch will choose the case (7) and it will execute the code a=0; b=0; c=0; break;.
switch(sequence){ //Selection by the number case(1): //if variable 'Sequence' has the value '1' b=0; c=0; break; //then turn on these segments (by turning off the port) case(2): //if variable 'Sequence' has the value '2' a=0; b=0; g=0; e=0; d=0; break; //then turn on these segments (by turning off the port case(3): //if variable 'Sequence' has the value '3' a=0; b=0; g=0; c=0; d=0; break; //then turn on these segments (by turning off the port case(4): //if variable 'Sequence' has the value '4' f=0; b=0; g=0; c=0; break; //then turn on these segments (by turning off the port case(5): //if variable 'Sequence' has the value '5' a=0; f=0; g=0; c=0; d=0; break; //then turn on these segments (by turning off the port case(6): //if variable 'Sequence' has the value '6' f=0; e=0; d=0; c=0; g=0; break; //then turn on these segments (by turning off the port case(7): //if variable 'Sequence' has the value '7' a=0; b=0; c=0; break; //then turn on these segments (by turning off the port case(8): //if variable 'Sequence' has the value '8' a=0; b=0; c=0; d=0; e=0; f=0; g=0; break; //then turn on these segments (by turning off the port case(9): //if variable 'Sequence' has the value '9' a=0; b=0; c=0; d=0; f=0; g=0; break; //then turn on these segments (by turning off the port default: //if variable 'Sequence' has the value '0' a=0; b=0; c=0; d=0; e=0; f=0; //then turn on these segments (by turning off the port ; }
To see the entire code of the example mentioned above, click here to learn more.
Resources
- Switch Statements. Microchip Developer Help. Link.
techZorro's Index of Content
Keep Reading!
- Macros on C, how to create useful Aliases for the CodeMacros are tremendously helpful in saving time and clutter in code writing. This tutorial includes tutorials on C. Click here to read.
- Switch in C, the neat multiple decision maker in Control FlowIn Control Flow, Switch replaces multiples if's and merges into one elegant decision making solution. Click here to read.
- While loop in C language, the ‘ask first' of Control FlowWhile in C is great for creating loops using Control Flow. It's great for repeated tasks and counters! Click here to read more.
- Code Template, an easy guide for PIC Microcontrollers in CWriting a program with order is a time saver. Download today the code template for PIC microcontrollers for free. Click here to read more.
- Functions in C Programming LanguageFunctions lets you reutilize code efficiently and opens up libraries done by others. Click here to learn to use functions in C.
[…] making, there is Switch. Excellent for programming and to leave the clutter behind. Click in the link below to read about […]