   switch VS if (memory usage and speed)
  I was just wonderring, which of these is faster?...switch(torf){  case [0]:  break;  case [1]:  break;}if(torf){}else{}What about something like these:switch(torf){  case [0]:    if(anotherBool)    {    }    else    {    }  break;    if(anotherBool)    {    }    else    {    }  case [1]:    if(anotherBool)    {    }    else    {    }  break;  case [2]:    if(anotherBool)    {    }    else    {    }  break;}In a case such as if I am in a 'first person shooter' mode, or a 'user interface' mode and I am pressing the left and right mouse buttons, etc.  I first want to see which 'mode' I am in, and if I am in user interface mode, clicking the mouse will allow me to move around icons, equip things, etc, but if I am in first person shooter mode, pressing the left mouse button will shoot a gun or whatever.So my mouseUp and mouseDown functions look something like this:function onMouseDown(e){var newButtonState = (system.ui.mouse.button - buttons);for(var i1 = [0]; i1 < [3]; i1++){if (newButtonState & Math.pow([2],i1)){switch(i1){case [0]:break;case [1]:break;case [2]:break;}}}buttons = system.ui.mouse.button;}function onMouseUp(e){var newButtonState = (buttons - system.ui.mouse.button);for(var i1 = [0]; i1 < [3]; i1++){if (newButtonState & Math.pow([2],i1)){switch(i1){case [0]:break;case [1]:break;case [2]:break;}}}buttons = system.ui.mouse.button;}So now should I go through each of those case statements and put in an if(mode==0){//do this}else{//do this}or should I put another switch statementswitch(mode){ case 0: break; case 1: break;}What is the overhead like on multiple switch checks?  Like 2-3 or even 4 levels deep?  Are iterations better if the choices are only one or the other?   
  from what i've read, switch statements are more or less syntactic sugar, and will often generate the exact same code as if else type blocks   ;  The question is pretty much meaningless. You do not have that level of control over the generated code. The compiler is trying very hard to write something that has the same meaning as what you wrote, but which will run as fast as possible. This means, in particular, that there is no possible one-to-one mapping between your source code and the resulting machine code.Switch statements are typically represented in a few different ways, depending on the values of the switch constants - the number of different cases, the range of values corresponding to those cases, whether or not there is a 'default' case - and anything that the compiler might be able to reason out about which cases are most likely. If-else branching is usually represented directly by 'jumps' in the generated assembly, but if the two branches execute similar and simple code, the compiler might be able to replace it with some kind of 'conditional move' instruction or similar.If there are only two possibilities, there is no real reason to use a switch statement. If there are many, then there is no real reason to use an if/else if chain - unless of course you have to. (The cases don't always correspond to different values of a single expression, after all... sometimes you need more complicated logic.)Also, why are you putting all the numbers inside square brackets?   ;Quote:Original post by BUnzagaIn a case such as if I am in a 'first person shooter' mode, or a 'user interface' mode and I am pressing the left and right mouse buttons, etc.  I first want to see which 'mode' I am in, and if I am in user interface mode, clicking the mouse will allow me to move around icons, equip things, etc, but if I am in first person shooter mode, pressing the left mouse button will shoot a gun or whatever.Such code is going to be called once or at maximum a handful of times per frame. In the grand scheme of things that's completely irrelevant background noise - pick whichever one makes the code cleaner and easier for you.1. Make it work.2. Make it fast.If you try and jump straight to '2' then you'll just end up wasting your own time and getting nowhere. Get a working game first then if it's too slow run a profiler over it and optimise the (usually two or three) bottlenecks it highlights.   ;  As far as I know, a switch statement (used properly_ will produce faster code than an if-else chain, because the switch statement uses a jump table to go to exact block of code, instead of evaluating each if-else before jumping.  If I remember correctly, the condition is that the case statements need to follow a linear progression - if you just shove in random values it'll defeat the purpose.  Ie:  case 0,1,2,3,4,5,6... will outperform an if-else chain, but case 193,921,813,294,845,901,.. won't, and degenerates into if-else.Again, this is because of the switch statement constructing a jump table using the provided value as a sort-of indexer.http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx;  Zahlman:It automatically did the bracket thing, I think because I set it to &#106avascript.  When I typed the code, there were no brackets.OrangyTang:I have made a few games, I just had the thought as I was re-writing for the nth number of times my user interface script.  I like to program like this:1. make it work, fast. :)Grafalgar:Awesome explination, and great article to back it up, that is exacty what I was looking for.   ;Quote:1. make it work, fast. :)That's a highly ambiguous statement: can you make it work fast fast? :)   ;  It also seems depends on the values your testing for. switch stements seem to have a fairly contant speed regardless of which case statement matches your value. An if, else if, else chain how will be significantly faster if one of the first items mathes eg.if(x == 5){...}//if 90% of the time this is the case then it may be faster than a switchelse if(x == 3){...}else if(x == 1){...}else if(x == 6){...}else if(x == 9){...}else {...}//if 90% of the time you end up here switch will be much fasterHowever when I tested this even in the case where 100% of the values matched the first if it was only fractional faster, although in the worst (ie ending up at the else{...}) case it was many times slower.Unless you can pretty much garuntee most of the items are matching the first if statement your probebly best off with using the switch statement.   ;Quote:Original post by BUnzagaZahlman:It automatically did the bracket thing, I think because I set it to &#106avascript.  When I typed the code, there were no brackets.OrangyTang:I have made a few games, I just had the thought as I was re-writing for the nth number of times my user interface script.  I like to program like this:1. make it work, fast. :)Um, you're using &#106avascript for UI handlers...The correct answeris therefore:inputController.onMouseUp(e);Then set inputController to either FPS mode or UI mode, with each one being implemented by its own handler.Even if user is clicking/typing really fast, this whole thing will be called some 10-50 times a second.And unless you're using SquirrelFish or v8, interpreter is running 10x slower than it should anyway, so until you switch to those two, it's all a moot point.   ;  If you want to switch between different modes I suggest using states instead of a massive switch statement in your input/game loop code.   