Switch statement in C

Switch Statement in C

most flexible program control statement is the switch statement, which lets your program execute different statements based on an expression that can have more than two values. Earlier control statements, such as if, were limited to evaluating an expression that could have only two values: true or false. To control program flow based on more than two values, you had to use multiple nested if statements. The general form of the switch statement is as follows:

  //syntex
  switch (expression)
  {
      case  template_1: statement(s);
      case  template_2: statement(s);
      ....................
      case  template_n: statement(s);
      default: statement(s);
  }

In this statement, expression is any expression that evaluates to an integer value: type long, int, orchar. The switch statement evaluates expression and compares the value against the templates following each case label. Then one of the following happens:

flow of Switch Statement

switch statement flow diagram AndroWep-Tutorials

Switch Statement in C language

  #include 
  int main()
  {
       int num=2;
       switch(num+2)
       {
           case 1:
             printf("Case1: Value is: %d", num);
           case 2:
             printf("Case1: Value is: %d", num);
           case 3:
             printf("Case1: Value is: %d", num);
           default:
             printf("Default: Value is: %d", num);
      }
      return 0;
  }

Both statement1 and statement2 can be compound statements or blocks.

Code Example Switch Statement

   /* Demonstrates the switch statement. */ 2: 3:
   //puts like as printf
   #include<stdio.h>
      int main( void )
      6:  {
      7:      int reply;
      8:
      9:      puts(“Enter a number between 1 and 5:”);
      10:     scanf(“%d”, &reply);
      11:
      12:     switch (reply)
      13:     {
      14:         case 1:
      15:             puts(“You entered 1.”);
      16:         case 2:
      17:             puts(“You entered 2.”);
      18:         case 3:
      19:             puts(“You entered 3.”);
      20:         case 4:
      21:             puts(“You entered 4.”);
      22:         case 5:
      23:             puts(“You entered 5.”);
      24:         default:
      25:             puts(“Out of range, try again.”);
      26:     }
      27:
      28:     return 0;
      29: }

  

Code Example Switch Statement

 #include 
  int main()
  {
       int i=2;
       switch (i)
       {
          case 1:
             printf("Case1 ");
          case 2:
             printf("Case2 ");
          case 3:
             printf("Case3 ");
          case 4:
             printf("Case4 ");
          default:
             printf("Default ");
       }
      return 0;
  }

What is switch statement in C language?

C – switch statement. Advertisements. witch statement flow diagram AndroWep – TutorialsA switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

How do you write a switch statement in C?

  • An expression must always execute to a result.
  • Case labels must be constants and unique.
  • Case labels must end with a colon ( : ).
  • A break keyword must be present in each case.
  • There can be only one default label.
  • We can nest multiple switch statements.