Switch Statement in C

 

                           C-Programming                   

                          DECISION MAKING AND BRANCHING

The Switch Statement-

 

  •  The switch statement used when we have many alternatives (options) and allows us to execute one alternates then used switch statement.
  • It is a selection case statement.
  • The switch statement is a substitute of if-else-if ladder statement.
  • The syntax of the switch statement is much easier to read and write.
  • The general form of the switch statement as-

           Syntax:

 

switch (expression)
{
      case value-1:

           statement block-1;
           break;

      case value-2:

           statement block-2;
           break;

      . . . . . .

      . . . . . .

 

      case value-n:

           statement block-n;
           break;
       default:

           default-block;
           break;

}

statement-x;

…..

 

 

Here, switch, case, break, default are keywords.

 

The expression should have a result of an integer or character value.

In expression not allowed float or string type.

 

value-1, value-2, . . . value-n are label values (constants) and

followed by colon (:) no space follows between label value and colon.

Label values define using case keyword.

 

Each of case label value should be unique inside a switch statement.

 

The break statement is used to exit (break out) from switch. It is optional.

That is two or more case labels may belong to the same statements.

If there is no break statement found in the case, all the cases will be executed
present after the matched case.

 

The default label is optional. If present, it will be executed when the expression
does not find a matching case label.

 

 

Working Process:

  • The Switch is a multi-way decision making statement.
  • In switch statement we pass an expression (int or char type) (condition- variable).
  • Expression value match a list of case label values, if match is found then a block of statements associated with that case is executed.
  • If matching is not found then default block will be executed. It is optional.

 

When we use switch … case statements?

  • It is used when there are multiple values for a variable.
  • When one of the many alternatives is to be selected, we can use a built-in multiway decision statement known as a switch.
  • The switch statement is often used for menu selection.
  • Also known as Conditional constructs.

 

Note:

  1.              ANSI allowed us to maximum 257 case labels in switch statement.
  2.                Switch statement may be nested (switch within switch). It is, a switch may
  3.      be part of a case statement. ANSI C permit 15 levels of nesting.

 

 

 

 

 

Example Program -1:

Write a program to read day number from keyboard display day name in word using switch statement.

/* C-Program read day number from keyboard display day name in word*/

#include<stdio.h>

int main( )
{   

      int n;
      printf("Enter day number :");  
      scanf("%d", &n);
      switch(n)
      {
         case 1:
              printf("Monday \n");  
              break;
         case 2:
              printf("Tuesday \n");
              break;
         case 3:
              printf("Wednesday \n");
              break;
         case 4:
              printf("Thursday \n");
              break;
         case 5:
              printf("Friday \n");  
              break;
         case 6:
              printf("Saturday \n");
              break;
         case 7:
              printf("Sunday \n");
              break;
         default:
              printf("invalid day number \n");
              break;
      }
      return 0;
}

Output 1:

Enter day number: 4
Thursday

Output 2:

Enter day number: 9
Invalid day number

 

 

Leave a Reply