Switch-case statement in C

On this page, you will learn about the switch-case statement and what the types of switch-case statements are in C programming. You will get a brief description of single switch statements and nested switch statements. In addition to this, you will get information about how to write single switch statements and nested switch statements. Further, you will get an example of single switch statements and nested switch statements.


What is the switch-case statement in C programming?

Switch case statement is a control flow statement in C programming that allows the program to execute different actions based on the value of an expression or variable. It is similar to a series of if-else statements, but provides a more concise and efficient way to compare multiple values.

The syntax for a switch case statement in C is as follows:


  switch(expression) {
    case value1:
       // code to be executed if expression matches value1
       break;
    case value2:
       // code to be executed if expression matches value2
       break;
    ...
    default:
       // code to be executed if expression does not match any case
 }

The expression is evaluated once and then compared with each of the case values. If there is a match, the code under that case statement is executed until the break statement is encountered. If there is no match, the code under the default statement is executed.

The switch case statement is often used in situations where there are many possible values for a variable, and each value requires a different action to be taken. This includes things like menu options in a program, days of the week, or different types of input data.

*** One important thing to note about switch case statements is that the expression being evaluated must be an integer, character, or enumeration type. It cannot be a floating-point number or string.

Switch-case statement flowchart example in C


What are the types of switch-case statement in C?

There are two types of switch-case statements in C: the single-switch statement and the nested switch statement.

  1. Single Switch Statement: The single switch statement is the most basic form of the switch-case statement. It consists of a switch keyword followed by a set of curly braces. Inside the curly braces, you list a series of case labels, each followed by a block of code to execute if the expression matches the case label. Here's an example:

    
      #include<stdio.h>
        int main() {
          int num = 3;
          
          switch(num) {
            case 1:
              printf("Number is 1");
              break;
            case 2:
              printf("Number is 2");
              break;
            case 3:
              printf("Number is 3");
              break;
            default:
              printf("Number is not 1, 2, or 3");
              break;
          }
          
        return 0;
      }
    
    
    Output:
    
      Number is 3
    
    
  2. Nested Switch Statement: The nested switch statement is a switch statement inside another switch statement. This allows for more complex decision-making and can be useful when dealing with complex data structures. Here's the syntax of nasted switch statement:

    
      switch (expression1) {
        case 1:
          switch (expression2) {
            case 1:
              // code to execute when expression1 is 1 and expression2 is 1
              break;
            case 2:
              // code to execute when expression1 is 1 and expression2 is 2
              break;
          }
          break;
        case 2:
          // code to execute when expression1 is 2
          break;
      }
    
    
    In the syntax, the outer switch statement evaluates expression1, and if it matches case 1 or 2, the corresponding block of code is executed. If it matches case 1, the inner switch statement is evaluated to determine which block of code to execute based on the value of expression2.

    Here is an example of nested switch statement:

    
      #include <stdio.h>
      int main() {
        int x = 1;
        int y = 2;
        int operation = 1;
        int result;
        switch (x) {
          case 1:
            switch (y) {
              case 1:
                result = x + y;
                break;
              case 2:
                result = x - y;
                break;
              default:
                printf("Invalid value of y");
                return 0;
            }
            break;
          case 2:
            switch (y) {
              case 1:
                result = x * y;
                break;
              case 2:
                result = x / y;
                break;
              default:
                printf("Invalid value of y");
                return 0;
            }
            break;
          default:
              printf("Invalid value of x");
              return 0;
        }
        printf("The result is: %d", result);
        return 0;
      }
    
    
    Output:
    
      The result is: -1
    
    

Some keynotes about switch-case statements in C are:

  1. The switch-case statement is used for multiple conditional branches in a program.
  2. It is a type of control statement that allows the program to choose from multiple options based on the value of a single expression.
  3. If a match is found, the corresponding case statement is executed. If no match is found, the default statement is executed.
  4. The break statement is used to terminate a case and exit the switch statement. If a break statement is not used, the control flow will continue to the next case.
  5. It is possible to nest switch statements inside other switch statements, which is called a nested switch statement.
  6. The switch-case statement can be an efficient alternative to an if-else statement when there are multiple conditions to be evaluated.
  7. It is important to make sure that each case statement is unique and that there is a default statement to handle any unexpected input.
  8. The switch-case statement is a powerful tool that can help make code more efficient and easier to read, but it should be used with care to avoid confusion or errors in program logic.


Finally, when using switch case statements in C, there are a few keynotes to keep in mind. First, it's important to note that duplicate case values are not allowed. Additionally, the default statement is optional. If a switch case statement doesn't have a default statement, it will run without any issues.

To terminate a statement sequence within the switch case block, the break statement is used. When the compiler encounters a break statement, it terminates the switch statement and jumps to the next line following the switch.

Nested switch statements are allowed, which means you can have a switch statement inside another switch. However, it's generally recommended to avoid nested switch statements because they can make a program more complex and less readable.