Switch Statement


The switch statement is another conditional structure. It is good alternative of if else if ladder statement. It can be used easily when there are many choices available and only one should be executed.

SYNTAX:
The following is the syntax of switch statement:

Switch (expression)

{

case val1:

   statements1;

break;

case val2:

    statements2;

    break;

    :

    :

case valn:

    statement;

    break;

default:

    statements;

}


Flowchart of switch statement:

Consider the following diagram in which the structure of the switch statement is defined:

Flowchart of switch statement


Working switch statement:

Switch statement compares the result of a single expression with multiple cases. Expression can be any valid that results in integer or character value. The expression is evaluated at the top of switch statement and its result is compared with different cases. Each case label represents one choice. If the result matches with any case, the corresponding block of statements is executed. Any number of cases can be used in one switch statement.

The default label appears at the end of all case labels. It is executed only once when the result of expression does not match with any case label.

The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.

NOTE: Remember that we cannot check a range of values using the switch statement like we did in the “if else if” ladder statement.


Points to remember:

The following points should be kept in mind while using switch statement:

  1. Don’t use expressions that result in any value of float or string data type.
  2. The break statement is optional in every case. If break statement is not used then all the cases will be executed even if the match is found.
  3. The default statement is also optional. We can use it anywhere in the switch cases and if it is not use there will be no affect on the switch statement.


Example of switch statement:

Consider the following code in which the switch statement is used to display the name of the fruits according to the number entered for example 1 for Apple, 2 for Mango and so on.

# include <stdio. h>

# include <conio. h>

void main ()

{

int n = 3;

switch (n)

{

case 1:

printf (“Apple”);

break;

case 2:

printf (“Mango”);

break;

case 3:

printf (“Banana”);

break;

case 4:

printf (“Peach”);

break;

case 5:

printf (“Avocado”);

break;

case 6:

printf (“Apricot”);

break;

case 7:

printf (“Melon”);

break;

default:

printf (“out of range”);

}

OUTPUT:

Banana

In the above example an integer type variable is declared named “n”. The variable n is initialized with value 3. The control enters the switch statement, the switch statement takes the value of n and compares it with all the cases when the value of n is matched with any of the case the statements in the body of that case will be executed and then the break statement is also executed. The message is printed and the control moves out of switch statement.