Pages

Wednesday, 9 May 2018

switch statement in C – Part 3


switch statement in C – Part 3


Read: switch statement in C – Part 2
In the last tutorial I told you about the practical use of switch keyword. As I said earlier a programmer generally use this keyword for menu driven programs. Today I will tell you about the tips and traps while using switch keyword. I will also compare switch with if-else ladder.

So lets start it one by one.

switch statement in C
1. It is not compulsory to make switch-case statement for only integer expression. We can also make it for characters too.


#include<stdio.h>

void main()
{
 char i='z';
 switch(i)
 {
  case 'a':
  printf("This will print a");
  break;

  case 'z':
  printf("This will print z");
  break;

  case 'p':
  printf("This will print p");
  break;

  default:
  printf("Sorry its a mismatch");
 }
}
#include<stdio.h>

void main()
{
 char i='z';
 switch(i)
 {
  case 'a':
  printf("This will print a");
  break;

  case 'z':
  printf("This will print z");
  break;

  case 'p':
  printf("This will print p");
  break;

  default:
  printf("Sorry its a mismatch");
 }
}