Control Statements in C Programming Language: Switch Statement

Switch statements are also decision-making statements which choose to execute a particular block of statements from many cases that has been provided in the code by the programmer. It is also known as switch-case-default. These programs are menu driven, i.e., it compares the value to the case values and executes the statements of the case that matches. The three keywords used are switch, case and default and they are used to make up the whole switch-case-default structure.

The flow chart below shows us the operation of a switch-case statement:

switch-case-flow-chart

Let us have a look at this simple switch-case-default program which will help us get a better understanding:


#include <stdio.h>

int main()
{
    int ch, a=16, b=5;
    double result=0.0;
    printf("Enter 1 for addition, 2 for subtraction, 3 for multiplication and 4 for division :\n");
    scanf("%d",&ch);
    
    switch(ch)
    {
        case 1:
            result=16+5;
            printf("The sum is: %f \n",result);
            break;
        case 2:
            result=16-5;
            printf("The difference is: %f \n",result);
            break;
        case 3:
            result=16*5;
            printf("The product is: %f \n",result);
            break;
        case 4:
            result=16/5;
            printf("The quotient is: %f \n",result);
            break;
        default:
            printf("Wrong Choice\n");
    }
    return 0;
}


Output:-
Enter 1 for addition, 2 for subtraction, 3 for multiplication and 4 for division : 
2
The difference is : 11.0000

 

Guidelines for Switch-Case

  • Cases can have integer values as well as char values for comparison and is followed by a colon(:).
  • The first thing to be kept in mind before writing a switch-case program is that a Switch statement can have any number of cases and they do not necessarily need to be arranged alphabetically or in the numerical order.
  • The different cases should be separated by the break statement otherwise the control will flow from one case to the other. This is known as fall through i.e. if a case is matched then it will execute the code of all cases below it until it finds a break statement or switch statement ends.
  • If we need to execute multiple statements in a case, we do not need to put the statement in blocks. The statements will get executed sequentially unless a break is encountered.
  • A Switch-Case statement may or may not have a default statement. If it doesn’t have a default statement the control will automatically go out of the switch scope and the next statement will be executed.
  • If a statement is written outside a case, it won’t be reported as an error. It won’t get executed because the statements inside switch get executed only if a case is matched.
  • A switch can also occur inside a switch which is known as nested switch but in practice, it is hardly implemented.

Break and Default statements

A break statement is used in switch-case at the end of a case. It terminates the case and takes the control outside of the switch block when encountered. If a case does not have a break at the end the control flows to the next case or default statement. This is known as fall through(explained above). Let us see how break works and also fall through in the following program:


#include <stdio.h>

int main()
{
    int ch;
    printf("Enter 1 for square, 2 for triangle, 3 for rectangle and 4 for circle :\n");
    scanf("%d",&ch);
	
    switch(ch)
    {
    case 1:
        printf("You chose Square\n");
        break;
    case 2:
        printf("You chose Triangle\n");
    case 3:
        printf("You chose Rectangle\n");
        break;
    case 4:
        printf("You chose Circle\n");
        break;
    default:
        printf("Wrong Choice\n");
    }
    return 0;
}


The number entered is 1.

Output:-
You chose square

When we enter 2. We see a fall through of control between cases and the case 3 is also executed because of the absence of break.

Output:-
You chose Triangle
You chose Rectangle

NOTE: – If we use a ‘continue’ statement in place of break it would not take the control back to the beginning of the switch as expected because switch is not a looping statement. It mostly shows an error.

Default statement in switch-case is executed when none of the cases matches. It is written in the same way as a case and is usually written at the end of the switch block. It does not need to be followed by a break statement. If the default statement is missing then the program won’t show any error but if none of the cases match the control will directly flow outside the switch block. In the above program of we enter 6, we get the output as:

Output:-
Wrong Choice

Switch-Case vs If-Else

To read in detail about if-else statement go to –  If-else Control Statements .

  • We cannot use variable expressions in switch like case x+5: or case i>7: , whereas if can have expressions like this.
  • Switch is a more structured when it comes to programming, also the level of indentation is manageable when compared to if-else.
  • One case cannot have the same expression as the other one.
  • Switch can only take numerical or character values for testing, it cannot have a float expression.
  • If there are many cases to be compared then using switch-case is more convenient.

An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.

Do not forget to share and Subscribe.

Happy coding!! 🙂

Recommended -