switch statement in C Advanced

In the last instructional exercise, I enlightened you concerning the commonsense utilization of switch watchword.

As I said before a software engineer by and large utilize this catchphrase for menu-driven projects.

Today I will educate you concerning the tips and traps while utilizing switch catchphrase. I will likewise contrast switch and if-else stepping stool.

So let’s start it individually.

switch articulation in C

It isn’t obligatory to put forth a switch-defense explanation for just whole number articulation.

void main()
 char i='z';
  case 'a':
  printf("This will print a");
  case 'z':
  printf("This will print z");
  case 'p':
  printf("This will print p");
  printf("Sorry its a mismatch");


This Will Print z

We can likewise make it for characters as well.

void main()
 char ans='Y';
  case 'y':
  case 'Y':
  printf("It will check both small and capital Y alphabet");
   case 'n':
  case 'N':
  printf("It will check both small and capital N alphabet");


It will check both small and capital Y alphabet

In the above program, we have rejected the default section.

As I said before its totally discretionary. You can likewise compose various articulations inside each case without stressing over the props.

In the event that in the event that you compose guidance inside switch case yet it doesn’t have a place with any case. At that point, the compiler will avoid that guidance at run time.

Switch articulation can likewise be utilized to check the aftereffect of specific whole number articulation. For example switch(150+8*0)

You can likewise compose settled switch proclamation however by and by it is utilized once in a while.

switch versus if-else

Points of interest in utilizing switch explanation

It is a much better method for composing a program and it likewise gives all around organized approach to programs.

Switch articulations work quicker than if-else stepping stool in light of the fact that while the accumulation of program, compiler,

for the most part, creates a hopping table by which it can without much of a stretch check the appropriate response as opposed to checking each condition.

Burdens of utilizing switch

It doesn’t permit to compose conditions with administrators.

It doesn’t permit to compose in any event, gliding point articulation. for example switch (0.5) isn’t permitted.

A developer can’t compose various cases which will give the same outcome.

case 8:



case 7+1:



Above code will give a mistake. Since two cases will give a similar outcome for example 8.

Leave a Comment

error: Alert: Content is protected!!