Jump to content

Switch vs If ?


makkawi

Recommended Posts

You use if...else if...else when you need to test different variables, and you use switch() when you want to test a single variable against multiple values.In addition, switch() allows some very interesting constructs when omitting the break statement before a certain case. In particular:

switch(var) {  case 1:  case 2:	//This will execute if var is 1 or 2.	break;  case 3:	//This will execute if var is 3.  case 4:	//This will execute if var is 3 or 4 (if var is 3, execution will start at the previous code block and continue to this one)	break;  default:	//This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4)  case 0:	//This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4) or 0 (if var is 0, execution starts here, otherwise at the previous code block)	break;}

The equivalent in if...else if...else would be:

if(var == 1 || var == 2) {  //This will execute if var is 1 or 2.}else if(var === 3 || var === 4) {  if (var == 3) {	//This will execute if var is 3  }  //This will execute if var is 3 or 4 (if var is 3, execution will start at the previous code block and continue to this one)}else {  //This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4)  if(var == 0) {	//This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4) or 0 (if var is 0, execution starts here, otherwise at the previous code block)  }}

Link to comment
Share on other sites

Remember the part I said about:

}else {  //This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4)  if(var == 0) {	//This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4) or 0 (if var is 0, execution starts here, otherwise at the previous code block)  }}

Actually, it's not equivalent. In swich(), if var is anything else, even the "0" code block will be executed. Here, it will not. To achieve a real if...else if... else equivalent, you need to use a function:

}else {  function ifVarIsNothingFromTheAbove() {	//This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4) including 0  }  if(!var == 0) {	//This will execute if var is nothing from the above (i.e. is not 1, 2, 3 or 4)	ifVarIsNothingFromTheAbove();  }else {	ifVarIsNothingFromTheAbove();  }}

This essentially makes the code slower (or at least it should) as the act of creating and calling a function is more expensive than a single simple condition evaluation.The differences between switch() and if...else if...else are small really. The only thing that I sometimes don't like about switch() is that is forces loose typing (at least in JavaScript and PHP), i.e.

var == 0

is the same as

var == "0"

I'd have loved if I could adjust this behaviour to scrict type checking where

var === 0

is not the same as

var === "0"

Actually wait... does JavaScript support sctrict type checking? The spec implemented in browsers that is (ECMAScript 4 probably does, but that's like a brand new thing).

Link to comment
Share on other sites

Actually wait... does JavaScript support sctrict type checking? The spec implemented in browsers that is (ECMAScript 4 probably does, but that's like a brand new thing).
If I found the correct spec, yes. Ctrl+F for "===" at http://www.ecma-international.org/publicat...ST/Ecma-262.pdf.But I thought even 0 would start at the default block?
Link to comment
Share on other sites

But I thought even 0 would start at the default block?
You know, you may have a point there... I didn't really tested out my code. I just thought about all possible combos and this one springed into mind.In that case, I guess the one other combo that would work (properly) would be:
  case 0:	//This will execute if var is 0  default:	//This will execute if var is nothing from the above (in which case execution will begin here) or 0 (in which case execution starts at the previous code block)

Did I missed anything?

Link to comment
Share on other sites

First, what console - Firefox's?And did you actually use a switch-statement with that? If not, you're apparently using an assumption I never have before, although I can't identify it immediately. I always thought of the interpreter scanning the cases until it found one that fit.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...