Switch Case in C

Switch Case in C


What is Switch Statement in C ?

This is one of a controlling statement in C. In an easy language it is a simplified version of if else statement. When we have multiple if conditions and we need to execute a different task for each condition then instead of using if else we use Switch Case Statement.

Why do we need a Switch Case ? 

There are no basic difference between “if-else” and “switch case statement” , whatever you can do with if-else statement , you can also do that same task with switch case.

So why we use switch case instead of if-else ?
Here is the answer :
1. When you have multiple conditions and if you use if-else again and again, your program might become messy.
2. Switch Case looks clean and clear rather than multiple if-else statement; so it generates less complexity.
3. Maximum time switch case statement takes less execution time compared to if-else statement.

Rules for switch statement :

1. Switch expression must be an integer or a character, floats are not allowed here.
2. Case value must be an integer or a character.
3. Case value must be end with a colon (:).
4. Case value must be used only inside of the Switch Statement.
5. We need to use unique case values, means they need to be different from each other. Otherwise it may create problems in program.
6.break” keyword is not mandatory but necessary. Otherwise program may be not able to give desired output.
7. We need to use a default statement at the end of all cases.

Switch Case Syntax :

switch(expression)
{
    case constant 1 :
    			//statement;
                	break;
    case constant 2 :
    			//statement;
                	break;
    case constant n :
    			//statement;
                	break;
    default :
    			//statement;
}

Switch Case Program :

Example :
#include<stdio.h>
int main()
{
    int number;
    printf("Choose number between 5 to 9 : ");
    scanf("%d",&number);
    switch(number)
    {
    case 5 :
    		printf("your value is : 35");
               	break;
    case 6 :
    		printf("your value is : 47");
               	break;
    case 7 :
    		printf("your value is : 56");
               	break;
    case 8 :
    		printf("your value is : 211");
               	break;
    case 9 :
    		printf("your value is : 169");
               	break;           
    default :
    		printf("Wrong choice !!");
    }
    return 0;
}
Output :
Choose number between 5 to 9 : 7
your value is : 56

Nested Switch in C :

Nested Switch statement is nothing but a different switch statement inside of another switch statement.

Syntax :
switch(expression)
{
    case constant 1 :
    //starting nested switch
    		switch(expression)
                {
    			case constant 7 :
    				//statement;
                			break;
    			case constant 8 :
    				//statement;
                			break;
                        default :
    				//statement; 
                }
    break;            
    case constant 2 :
    			//statement;
                	break;
    case constant n :
    			//statement;
                	break;
    default :
    			//statement;
}