What is switch case statement in c

What is switch case statement in c

What is the switch case statement in c

C switch case statement is a multipath decision making that allows to selection and execution of a particular block of statements from several blocks of a statement based upon the value of the expression, which is included within the switch statement and branches accordingly. The expression must be of an integer value (“integer” values are simply values that can be expressed as an integer, such as the value of a char).

The same task can be performed using the if-else ladder as well but as the number of alternatives increases, the selection process becomes more complex (more consuming). The main difference between the if-else statement and the switch statement is in the switch case statement it is done in a parallel fashion. So switch statement is much faster than if-else ladder. 

The switch statement body consists of a series of case labels and optional default labels. The default label can appear only once and anywhere in the body of the switch statement.

When there are a number of else alternatives, a switch statement is another way of representing this multiway selection. A switch statement allows the user to choose a statement among several alternatives.   

 Syntax of switch case statement in C

switch(expression)

{

    case(case constant 1):

{
      statement 2;

      break;

}

    case(case constant 3):

{
      statement 3;

      break;

}

    case(case constant n-1):

{
      statement n-1;

      break;

}

    case(case constant n):

     Statement n;

break;

}

default:

{
   statement

}

}

Syntax Description

  •          When the switch statement is executed, the value of an expression is compared with the value of case constant (constant 1, constant 2……………)
  •         Then the block of statements associated with the case whose value is matched with expression will be executed.
  •           break statement at the end of each block indicates the end of the particular case and causes an exit from the switch statement and control will be transferred ta statements following the switch.
  •        If none of the case match with the value of the expression. Then, the block of a statement under default is executed.

 

Flow chart of switch case statement in C

 

Flow chart of switch case statement


Example

Write a C program to print name of day entering the number from 1 to 7 using switch case statment  

#include<stdio.h>

#include<conio.h>

void main()

{

                int z;

                printf("\n choose a number (1 to 7)");

                scanf("%d", &z);

                switch (z)

                {

                case 1:

                                {

                                                printf("\n Sunday");

                                                break;

                                }

                case 2:

                                {

                                                printf("\n Monday");

                                                break;

                                                }

                case 3:

                                {

                                printf("\n Tuesday");

                                break;

                                }

                case 4:

                                {

                                printf("\n Wednesday");

                                break;

                                }

                case 5:

                                                {

                                                printf("\n Thusday");

                                                break;

                                                }

                  case 6:

                                                {

                                                printf("\n Friday");

                                                break;

                                                }

                case 7:

                                                {

                                                printf("\n Saturday");

                                                }

                default:

                               {                                                                                                                                                                               

                                printf("\n Invalid input");

                          }

                }

getch();

 }

 Output

Output of switch case statement


 Here in the program, the user has to enter 1 to 7 to display the respective day. The variable z holds 1 to 7 numbers and the data type of z is an integer. If the variable z holds characters such as alphabets, symbols, etc. we have to define z as a character.

When the user enters an expression, the first value of the expression is compared with the value of case 1 constant if it is true the case 1 will be executed. break stops the program that means if case 1 is executed the program does not go further. 

If case 1 is not satisfied then the expression is compared with the value of case 2 constant. If it is true, case 2 will be executed and break stops the program. If it is the false value of an expression is compared with the value of case 3. Continuously, the program goes.

If none of the cases match with the value of the expression, then the block of a statement under default is executed.

Now, in the above program when the user enters 1, the program display ‘Sunday’, if the user gives 2, it prints ‘Monday’ similarly if the user gives 7then ‘Saturday’ is print.

When the user gives except 1 to 7, the default will be executed.


 Let see another example

Write a C program to find sum, different and product of two numbers using switch case statement

#include<stdio.h>

#include<conio.h>

void main()

{

                char z;

                int a, b, s, d, p;

                printf("\n Enter (+) for sum (-) for difference and (*) for product");

                scanf("%c", &z);

                switch(z)

                {

                case '+':

                {

                                printf("\n Enter two digit");

                                scanf("%d%d", &a,&b);

                                s=a+b;

                                printf("\n Sum=%d",s);

                                break;

                }

                case '-':

                {

                                printf("\n Enter two digit");

                                scanf("%d%d", &a, &b);

                                d=a-b;

                                printf("\n Difference=%d", d);

                                break;

                }

                case '*':

                {

                                printf("\n Enter two digit");

                                scanf("%d%d", &a, &b);

                                p=a*b;

                                printf("\n Product=%d", p);

                                break;

                }

                default:

                                {

                                                printf("\n Invalid input");

                                }

                }

                getch();

                }

Output


C program to find sum, different and product of two numbers using switch case statement



For the if-else statement

Click here

For the else if ladder 

Post a Comment

0 Comments