top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How should a programmer take decision over choice for "switch-case" and "if-else" ?

+3 votes

Sometime, I get confused which one I should take while writing code.
Is there any way, which can be help me while choosing either switch-case or if-else ?

posted Sep 5, 2014 by Neelam

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

1 Answer

0 votes

If depends on your problem statement.

Major Advantages of switch case :-
1>If you need more number of conditions need to be checked then always switch case will be better as in case of switch case it will directly jump to the index jump table , but if you have less conditions (< 5) need to be checked then if else is better.
2>switch case gives any way code clarity for reviewer

Constraint in switch case :-
1> Your switch variable (condition variable) has to be integer, but in if else any condition you can add.

If you interested how switch case creates jump table and directly jumps to the specified memory address to execute go through this wonderful article but basic assembly instruction need to be clear to refer.

answer Aug 14, 2015 by Sachidananda Sahu
Similar Questions
+1 vote

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

+2 votes

Can I assign one structure variable of another structure variable of same structure type all the time ?

+1 vote

Is there any logic or its just random behavior based on availability of RAT or based on any rat priority etc ?