C

C – Example Of A Switch Case Statement In C Programming

When we have multiple options and need to perform a different task for each option, we use the switch case statement.

Switch Case Statement (C)

Let’s look at the syntax of a switch case statement before we see how it works in a C program.

switch (variable or an integer expression)
{
     case constant:
     //C Statements
     ;
     case constant:
     //C Statements
     ;
     default:
     //C Statements
     ;
}

In C, an example of a switch case

Let’s look at a simple example to see how a switch case statement works in a C program.

#include <stdio.h>
int main()
{
     int num=2;
     switch(num+2)
     {
         case 1:
           printf("Case1: Value is: %d", num);
         case 2:
           printf("Case1: Value is: %d", num);
         case 3:
           printf("Case1: Value is: %d", num);
         default:
           printf("Default: Value is: %d", num);
    }
    return 0;
}

Output:

Default: value is: 2

Explanation: In switch, I used an expression, but you could also use a variable. I entered num+2, where num is 2 and the expression resulted in 4. Because no case with value 4 is defined, the default case is executed.

Story twist – Introducing the Break statement

Before we go any further into the break statement, try to guess the output of this C program.

#include <stdio.h>
int main()
{
     int i=2;
     switch (i)
     {
        case 1:
           printf("Case1 ");
        case 2:
           printf("Case2 ");
        case 3:
           printf("Case3 ");
        case 4:
           printf("Case4 ");
        default:
           printf("Default ");
     }
    return 0;
}

Output:

Case2 Case3 Case4 Default

I passed a variable to switch, and the variable’s value is 2, so the control jumped to case 2. However, there are no such statements in the above program that could break the flow after case 2. That is why, after case 2, all subsequent cases and default statements were executed.
How can this situation be avoided?
After each case block, we can use the break statement to interrupt the flow of control.

Switch Case Break Statement

When you want your program flow to exit the switch body, use break statements. The control exits the switch case statement whenever a break statement is encountered in the switch body.
Switch Case with Break Example
I’m taking the same approach as before, but this time we’re using break.

#include <stdio.h>
int main()
{
     int i=2;
     switch (i)
     {
          case 1:
             printf("Case1 ");
             break;
          case 2:
             printf("Case2 ");
             break;
          case 3:
             printf("Case3 ");
             break;
          case 4:
             printf("Case4 ");
             break;
          default:
             printf("Default ");
     }
     return 0;
}

Output:

Case 2

Why did I not use a break statement after the default?
I didn’t use it because the control would come out of the switch after default, but if you want to use the break after default, you can; there is no harm in doing so.

A Few Important Points Concerning the Switch Case

1) The order of the cases does not always have to be 1, 2, 3, etc. After the case keyword, they can have any integer value. Furthermore, cases do not always have to be specified in ascending order; you can specify them in any order required by the program.
2) Characters can also be used in switch cases, such as –

#include <stdio.h>
int main()
{
     char ch='b';
     switch (ch)
     {
         case 'd':
            printf("CaseD ");
            break;
         case 'b':
            printf("CaseB");
            break;
         case 'c':
            printf("CaseC");
            break;
         case 'z':
            printf("CaseZ ");
            break;
         default:
            printf("Default ");
    }
    return 0;
}

Output:

CaseB

3) The expression provided in the switch should result in a constant value otherwise it would not be valid.
As an example:
Valid switch expressions –

switch(1+2+23)
switch(1*2+3%4)

Invalid switch expressions –

switch(ab+cd)
switch(a+b+c)

4) Switch statements can be nested, which means they can be contained within another switch statement. Nested switch statements, on the other hand, should be avoided because they make the program more complex and less readable.
5) No duplicate case values are permitted. The following program, for example, is incorrect:
This program is incorrect because we have two case ‘A’ values, which is incorrect because we cannot have duplicate case values.

#include <stdio.h>
int main()
{
     char ch='B';
     switch (ch)
     {
         case 'A':
            printf("CaseA");
            break;
         case 'A':
            printf("CaseA");
            break;
         case 'B':
            printf("CaseB");
            break;
         case 'C':
            printf("CaseC ");
            break;
         default:
            printf("Default ");
    }
    return 0;
}

6) The default statement is optional; if the program does not have a default, it will function normally. However, it is best practice to have a default statement that executes if no case is matched. This is especially useful when we are taking user input for case choices; because users can sometimes enter incorrect values, we can remind them with a proper error message that we can set in the default statement.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button