5.14 – Switch – case statement

by subbu on September 25, 2013

The switch-case selection statement:

  • It is a selection statement used to select a choice from number of choices.
  • Keywords switch, case and default are used in defining switch-case structure.
  • The switch-case selection statement has the switch statement with an expression and number of case definitions with their constants.
  • Only integer and single character constants can be used as case constants.
  • Execution starts from the case whose constant matches with the value of switch expression.
  • If the value of switch expression doesn’t match with any of the case constants then the default case is executed.
  • break; statement is used to execute only a single case out of the total structure.
  • Defining the default case in switch-case control structure is optional.

switch case selection statement

#include<stdio.h>
int main()
{
int x=20;
switch(x+10)
{
case 20:
  printf("One");
case 30:
  printf("\nTwo");
case 40:
  printf("\nThree");
default:
  printf("\nFour");
}
return 0;
}

Output:
Two
Three
Four

Example explained:
The value of expression x+10 matches with case 30: hence case 30:, 40: and default cases are executed.

#include<stdio.h>
int main()
{
int x=20;
switch(x+10)
{
case 20:
  printf("One");
  break;
case 30:
  printf("\nTwo");
  break;
case 40:
  printf("\nThree");
  break;
default:
  printf("\nFour");
}
return 0;
}

Output:
Two

Example explained:
Because of the break; statement only case 30: is executed and came out of the total structure.

Specification:
Accept any integer and print whether the number is an even or odd

Using if-else selection statement:

if(n%2==0)
  printf("Even number");
else
  printf("Odd number");
/* Using switch-case control structure*/
#include<stdio.h>
void main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
switch(n%2)
{
 case 0:
   printf("Even number");
   break;
 case 1:
   printf("Odd number");
   break;
}
return 0;
}

Execution 1:
Enter any number: 25
Odd number

Execution 2:
Enter any number: 12
Even number

Specification:
Accept two numbers and print the biggest number.

Using else-if ladder selection statement:

if(a==b)
  printf("Equals");
else if(a>b)
  printf("Biggest number %d",a);
else
  printf("Biggest number %d",c);
/* using switch-case control structure*/
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
switch(a==b)
{
 case 1:
 printf("Equals");
 break;
 case 0:
 switch(a>b)
 {
 case 1:
 printf("Biggest number %d",a);
 break;
 case 0:
 printf("Biggest number %d",b);
 }
 }
return 0;
}

Execution 1:
Enter two numbers:
10 10
Equals

Execution 2:
Enter two numbers:
30 10
Biggest number 30

Note: in C language any relational expression returns either 1 or 0

Specification:
Accept any two numbers and print whether the sum, subtraction, product or division by taking choice from the user.

#include<stdio.h>
int main()
{
int x,y,ch;
printf("Enter two numbers:\n");
scanf("%d%d",&x,&y);
printf("1.Addition\n 2.Substraction\n3.Product\n4.Division\nEnter your choice 1..4:");
scanf("%d",&ch);
switch(ch)
{
  case 1:
     printf("Addition %d",x+y);
     break;
  case 2:
     printf("Subtraction %d",x-y);
     break;
  case 3:
     printf("Product %d",x*y);
     break;
  case 4:
     printf("Division %f",(float)x/y);
     break;
  default:
     printf("Invalid choice");
}
return 0;
}

Execution 1:
Enter two numbers:
7     2
1.Addition
2.Substraction
3.Product
4.Division
Enter your choice 1..4: 4
Division 3.500000

Execution 2:
Enter two numbers:
7     2
1.Addition
2.Substraction
3.Product
4.Division
Enter your choice 1..4: 8
Invalid choice

Specification:
Accept any alphabet and print whether it is a vowel or consonant.

#include<stdio.h>
int main()
{
char ch;
printf("Enter any alphabet:");
scanf("%c",&ch);
switch(ch)
{
  case 'a':
  case 'A':
  case 'e':
  case 'E':
  case 'i':
  case 'I':
  case 'o':
  case 'O':
  case 'u':
  case 'U':
     printf("Vowel");
     break;
  default:
     printf("Consonant");
}
return 0;
}

Execution 1:
Enter any alphabet: e
Vowel

Execution 2:
Enter any alphabet: P
Consonant

Specification:
Accept any three digit number and print it in words.

Show Program
#include<stdio.h>
int main()
{
int n,first,second,third;
printf("Enter any three digit number:");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
/* printing the hundreds */
switch(first)
{
  case 9:
     printf("Nine hundred");
     break;
  case 8:
     printf("Eight hundred");
     break;
  case 7:
     printf("Seven hundred");
     break;
  case 6:
     printf("Six hundred");
     break;
  case 5:
     printf("Five hundred");
     break;
   case 4:
     printf("Four hundred");
     break;
   case 3:
     printf("Three hundred");
     break;
   case 2:
     printf("Two hundred");
     break;
   case 1:
     printf("One hundred");
     break;
}
if(second!=0 || third!=0)
    printf( "and ");
/* printing tens */
switch(second)
{
   case 9:
     printf(" Ninety ");
     break;
   case 8:
     printf(" Eighty ");
     break;
   case 7:
     printf(" Seventy ");
     break;
   case 6:
     printf(" Sixty ");
     break;
   case 5:
     printf(" Fifty ");
     break;
   case 4:
     printf(" Forty ");
     break;
   case 3:
     printf(" Thirty ");
     break;
   case 2:
     printf(" Twenty ");
     break;
   case 1:
       switch(third)
       {
          case 0:
            printf(" Ten ");
            break;
         case 1:
            printf(" Eleven ");
            break;
         case 2:
            printf(" Twelve ");
            break;
         case 3:
            printf(" Thirteen ");
            break;
         case 4:
            printf(" Fourteen ");
            break;
         case 5:
            printf(" Fifteen ");
            break;
         case 6:
            printf(" Sixteen ");
            break;
         case 7:
            printf("Seventeen ");
            break;
         case 8:
            printf(" Eighteen ");
            break;
         case 9:
            printf(" Nineteen ");
            break;
       } /*end of inner switch*/
 }/*end of outer switch*/
/* printing ones */
if(second!=1)
{
  switch(third)
  {
     case 9:
        printf("Nine");
        break;
     case 8:
        printf("Eight");
        break;
     case 7:
        printf("Seven");
        break;
     case 6:
        printf("Six");
        break;
     case 5:
        printf("Five");
        break;
     case 4:
        printf("Four");
        break;
     case 3:
        printf("Three");
        break;
     case 2:
        printf("Two");
        break;
     case 1:
        printf("One");
        break;
    } /*end of switch*/

}/*end of if*/
return 0;
}

Execution 1:
Enter any three digit number: 345
Three hundred and Forty Five

Execution 2:
Enter any three digit number: 415
Four hundred and Fifteen

Execution 3:
Enter any three digit number: 400
Four hundred

Execution 4:
Enter any three digit number: 405
Four hundred and Five

blank line

Points to remember:

  1. Only integer and single character types are allowed
  2. Expressions can be used both as switch conditions and case constants but identifiers can’t be used in the expressions of case labels.
  3. When we use character constants then their ASCII values are actually compared.
  4. Duplicate case labels (values) can’t be used.

Quiz:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
float x=12.34;
switch(x)
{
  case 10.34:
    printf("Hello");
    break;
  case 12.34;
    printf("World");
    break;
  default:
    printf("Codingfox.com");
}
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=40,y=20,z=25;
switch(x+40)
{
  case y+z;
      printf("Hello");
      break;
  case 4*y:
     printf("World");
     break;
  default:
     printf("codingfox.com");
}
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=40;
switch(x+40)
{
  case 40+20:
    printf("Hello");
    break;
  case 40+40:
    printf("World");
    break;
  default:
    printf("codingfox.com");
}
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=8;
switch(x)
{
   case '8': /* ASCII value is 58*/
      printf("Hello");
      break;
   case 8:
      printf("World");
      break;
   default:
      printf("codingfox.com");
}
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=195;
switch(x)
{
   case 'a'+'b': /* 97+98*/
      printf("Hello");
      break;
   default:
      printf("codingfox.com");
}
return 0;
}

6) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=25;
switch(x)
{
  case 25:
    printf("Hello");
    break;
  case 10+15:
    printf("World");
    break;
  default:
    printf("codingfox.com");
}
return 0;
}

7) What would be the output of following program?


#include<stdio.h>
int main()
{
switch(20)
{
 default:
  printf("In valid case");
  break;
 case 10:
  printf("Ten");
  break;
 case 20:
  printf("Twenty");
}
return 0;
}

Show Answers
1) Error (Floating point type is not allowed in switch-case control structure)

2) Error (An identifier can’t be used in case label)

3) World (Expressions without identifiers can be used in case label)

4) World

5) Hello

6) Error (Duplicate case values can’t be used)

7) Twenty (default case can be defined any where within the switch-case statement)

Previous post:

Next post: