Important Notice: Effective immediately, all support will be handled through Discord. Read more about the changes coming to Anodyne's services.

What's better - if vs switch

Sometimes you need a place to chat about stuff, and that's okay, because we're a community. This board is meant just for that, to talk about everything and even your kitchen sink (if you feel so inclined).

mooeypoo Mon Sep 19, 2011 11:28 pm

What's better - if vs switch

Obviously an "if" statement is more robust for multiple conditions and such. But if the code of the moment involves a relatively simple procedure, which is preferable? "if" or "switch" ?

Example 'if':
Code: Select all
if ($var=='1') {
   //do something
} elseif ($var =='2') {
   //do something else
} elseif ($var=='3') {
   //do something compltely different
} else {
   //amaze us
}


Example of the same idea with 'switch':
Code: Select all
switch ($var) {
   case '1':
       //do something
       break;
   case '2':
       //do something else
       break;
   case '3':
       //do something compltely different
       break;
   default:
       //amaze us
       break;
}


The only thing I can immediately see is that 'switch' takes more statements than 'if', but I always felt it's more fluid, for no particular reason other than my own personal feel of it.

Obviously, there's a difference of function between the two as well. If, as I pointed out, can be a lot more robust in terms of conditions (and/or/both/combination).
For example, this will likely be best for an 'if' statement:
Code: Select all
if ((($val !== FALSE) || ($val2==TRUE)) && (!isset($val3))) {
 //do something
}


And 'switch' can be helpful when you have a specific variable with specific values, or if you need one statement to do partial operation before also doing another condition's functions, as in this example:

Code: Select all
switch ($val) {
   case '1':
       //do something but don't stop (no break), continue to also do what's in case 2
   case '2':
       //do more somethings
       break;
   default:
       //do only this thing on 'default'
       break;
}


So clearly there's a practical usage difference. That's fine. But if I need a simple if/elsif operation, like the first example -- is there any real difference between these two?

I am not sure there's a definitive answer.. I tried researching if there are any speed considerations and couldn't really find much other than difference int he amount of code written. I am not sure I want to take that as reason enough to pick 'if/else'. Code lines might save time while programming, but if one operation is more logical than the other one, it can save time later while tweaking and will make the code a lot more readable and easy to use.

So... what's your thoughts? Any preference to either one? Anyone knows if there's an actual difference in processing speeds or such? And if not, why would you choose either one and which is your favorite?

~mooey
Find my Nova MODs: https://github.com/mooeypoo
User avatar

Posts: 247


AgentPhoenix Tue Sep 20, 2011 9:47 am

Re: What's better - if vs switch

Honestly, I think it depends on how you want to build it. I don't think there's any one way that you should build something. I'm partial to switch statements myself, but that's just a personal preference. I've never seen a "guide" for when you're supposed to use one over the other.
User avatar

Posts: 7596



Return to The Anodyne Lounge

Who is online

Users browsing this forum: No registered users and 3 guests

cron