top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Limitations of switch case statement?

+1 vote
3,528 views

As I could guess that switch case should be avoided in C can someone explain in detail when not to use switch case statement?

posted Jul 7, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Switch statement is nothing but a multiple elseif statement with high level of readability. See the following example

switch (x)
{
  case 1:
     // statement 1
     break; 
  case 2:
     // statement 2 
     break; 
  case 3:
     // statement 3 
     break; 
  default:
     // statement 4 
     break;
}

Equivalent If-else-if statement

if(x==1)
{
  // statement 1
}
else if(x==2)
{
  // statement 2
}
else if(x==3)
{
  // statement 3
}
else 
{
  // statement 4
} 

Now coming to switch statement, switch statement i.e. multiple elseif statement should be avoided as it is processing intensive there are better ways t handle i.e. array of pointers and based on array index call specific processing.

answer Jul 7, 2014 by Salil Agrawal
0 votes

@Limitation of Switch case:
There are 2 mainly 2 limitations of switch case
a) switch (value)
value variable should be either integer or char.
b) In the case u can not use logical operators in cases.
like case a <2.
@As I could guess that switch case should be avoided in C can someone explain in detail when not to use switch case statement?
Already a good explanation and detailed answer was given by Sahil sir.

answer Jul 8, 2014 by Double S
...