• switch Statement –

      ‘C’ language provides a switch statement. In a complex problem, if many alternative values are available, then writing the code using multiple if…else becomes lengthy and also difficult to manage. At that time switch statement which is a multi-way decision statement is handy, because management of code becomes easy. 


Switch Statements in C

       The Flow chart of if-else statement is –

      switch (variable name or expression)

      {

                  case 1:

                             statement(s)1;

                             break;

                  case 2:

                             statement(s)2;

                             break;

                  .

                  . 

                  .

                  .

                  case N:

                             statement(s) N;

                             break;

                  default:

                             Default-Statement(s);

       } 

Here, statement(s)1 will be executed if the value of expression is case 1, similarly statement(s)2 will be executed if the value of expression is case 2. If the value of expression does not match any values from case 1 to case N, then the default-statement are executed.

It is to be noted that writing the default value and its corresponding default-statement is optional. The value of expression written after switch statement must be either character or integer value.

  • break Statement –

Break Statement In c

         We have used the break statement in switch statement. It causes an exit from the switch body. If it is not written after each case statement, then control passes to the next statement, so remaining statements of next case will also execute even if the case value do not match and the program will not function properly. 


  • default Statement –

            This statement is also used in switch statement. It is optional, but if it is used, then the statements written in the part get executed if any of the previous case values do not match. Normally, the default keyword is used in switch statement and written after all the cases.