What is the difference between if-else-if and switch statement?

Questions by shilpak10   answers by shilpak10

Showing Answers 1 - 6 of 6 Answers

RohithBlr

  • Jun 4th, 2007
 

  • "switch" statements are faster because with a switch, PHP compares an original value to a set of case values, the same holds with javascript... With an if else statement, it needs to do more parsing.

     
  • The differences here are going to literally un-noticable, and for general use, dont try and convert your if else statements to switch's unless a switch() loop is a more appropriate loop to use. given possibly different options each time

if statement can be used when much more conditions need to check. where as switch stmt can be used when we need to compare a single expression with various options.

Ex: if(studentMarks>70)
{
echo "Gread A";
}
elsif(studentMarks>60)
{
echo "Gread B";
}
elsif....

This logic, we are unable to write with Switch case

Means, we can't able to write the comparisions like in between range.


One more thing is in switch stmt we can't able to compare with float values, expressions..

Hope this will helpful to u
Regards,

Ganesh Kumar.G

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions