LERAN C PROGRAMMING

Learn one of the most powerful programming languages in the world and become a rockstar developer.

SWITCH CASE Stament

In this tutorial, you will learn to create the switch statement in C programming with the help of an example.

Switch case statements are a substitute for long if statements that compare a variable to several integral values

  • The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • Switch is a control statement that allows a value to change control of execution.

The break statement ends the loop immediately when it is encountered. Its syntax is:

switch (n)
{
    case 1:
		// code to be executed if n = 1;
        break;
		
    case 2: 
		// code to be executed if n = 2;
        break;
		
    default: 
		// code to be executed if n doesn't match any cases
}

Rules for using switch statement

  1. The expression (after switch keyword) must yield an integer value i.e the expression should be an integer or a variable or an expression that evaluates to an integer.
  2. The case label values must be unique.
  3. The case label must end with a colon(:)
  4. The next line, after the case statement, can be any valid C statement.

switch Statement Flowchart

c switch statement flowchart


Points to Remember

  • We don't use those expressions to evaluate switch case, which may return floating point values or strings or characters.
  • break statements are used to exit the switch block. It isn't necessary to use break after each block, but if you do not use it, then all the consecutive blocks of code will get executed after the matching block.
#include <stdio.h> 
int main() 
{ 
    int i = 1;
    switch(i)
    {
        case 1:    
            printf("A");        // No break
        case 2:
            printf("B");        // No break
        case 3:
            printf("C");
            break;
    }
}
Output :

A B C

The output was supposed to be only A because only the first case matches, but as there is no break statement after that block, the next blocks are executed too, until it a break statement in encountered or the execution reaches the end of the switch block.

  • default case is executed when none of the mentioned case matches the switchexpression. The default case can be placed anywhere in the switch case. Even if we don't include the default case, switch statement works.
  • Nesting of switch statements are allowed, which means you can have switchstatements inside another switch block. However, nested switch statements should be avoided as it makes the program more complex and less readable.
  • Example :

    // Following is a simple C program  
    // to demonstrate syntax of switch. 
    #include <stdio.h> 
    int main() 
    { 
       int x = 2; 
       switch (x) 
       { 
           case 1: printf("Choice is 1"); 
                   break; 
           case 2: printf("Choice is 2"); 
                    break; 
           case 3: printf("Choice is 3"); 
                   break; 
           default: printf("Choice other than 1, 2 and 3"); 
                    break;   
       } 
       return 0; 
    }
    Output :

    Choice is 2


    Previous Next
    x
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine