Switch in C, the neat multiple decision maker in Control Flow

Switch in C, the neat multiple decision maker in Control Flow

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.

https://www.techzorro.com/en/blog/control-flow-c/

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.

Switch using break. Respectfully borrowed from Microchip. Link.

If break is not placed, then it will jump into the next case automatically, as shown in this chart:

Switch without using break. Respectfully borrowed from Microchip. Link.

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!

One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.