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

Syntax of Switch

So, how does Switch in C programming language looks like?

C

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

C

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.

Don't know how to start?

Find out how you can learn step-by-step.

    Juditova

    Variable Frequency Drives: how this controller can transform induction motors forever

    Juditova         Juditova        
    Variable Frequency Drives: how this controller can transform induction motors forever           Variable Frequency Drives: how this controller can transform induction motors forever          
    Back 15 seconds
    Forward 60 seconds
    more
      Back 15 seconds
      Forward 60 seconds
      Currently Playing