there was a thread earlier today about a switch statement. If you want to know how that functions, I think it will give a little insight
…oops nevermind you already saw that thread haha ok Ill doa little writeup about the switch statement.
btw, for others, the thread is
http://www.kirupaforum.com/showthread.php?s=&threadid=15391
…
Switch
What it does
The switch action is an if-like action for comparing one expression with other multiple expressions. Switch offers nothing more then that which could be achieved with if clauses, though it does offer a more compact and concise alternative way of coding it. Switch however, only checks for equality, and strict equality ( === ) at that. This means that true is equal to true but true is not equal to 1 (1 is equal to true if == (equality) is used). There are no less thans or greater thans with switch. So how does it work?
Setup
Switch is comprised of a switch block which within contains multiple case blocks. The switch action maintains the original comparison value and each case contains the value to which it is to be compared. Following each case block are the commands which are to be run if the comparison between the switch value and the case value resolves to be true.
switch (expression) {
case (expression):
statements...
case (expression):
statements...
...
}
Heres a simple example in context
switch (myVar) {
case 1:
trace("myVar equals 1");
break;
case 2:
trace("myVar equals 2");
break;
case 3:
trace("myVar equals 3");
break;
default:
trace("No match was found");
}
This goes through and checks myVar to equal (strictly) each of the values following the case keyword within the switch block. Here we check for 1, 2 and 3. Each case is checked in order from top to bottom until one is found to be equal. When it is, the following code is execueted. If myVar was 1 here, “myVar equals 1” would appear in the output window since the case 1 clause was equal to myVar and the code was run. In terms of an if/else statement, the above would look like:
if (myVar === 1) {
trace("myVar equals 1");
}else if (myVar === 2){
trace("myVar equals 2");
}else if (myVar === 3){
trace("myVar equals 3");
}else{
trace("No match was found");
}
Default
The defauly case - which doesnt use the case keyword - contains the statements which are to be run if none of the previous case clauses evaluated to be true. If myVar was 4, the default clause would be run since neither of the previous case clauses were found to be equal to myVar. You dont have to use the default case if you dont want to. Its use is optional.
Break
The break statement exits the switch block. This is needed because if a case statement is equal to the switch expression, the statements in that case AND all following case statements are run. Using break will cause only that case statement to run and then skip all the rest since at that point you wont have to worry about the following cases. You may, however, not want to use break in some cases. For instance, you can check myVar to be either one value OR another value and have certain code run based on that. For example:
switch (myVar) {
case 1:
case 2:
trace("myVar equals 1 or 2");
break;
case 3:
case 4:
trace("myVar equals 3 or 4");
break;
default:
trace("No match was found");
}
Since there is no break after case 1: or case 3: if myVar is either of those values, it will run through the case statments associated with that case block (none) and continue to the next case block and run those statements (trace) until reaching the break which exits the switch block completely. As an if/else this would be:
if (myVar === 1 || myVar === 2) {
trace("myVar equals 1 or 2");
}else if (myVar === 3 || myVar === 4){
trace("myVar equals 3 or 4");
}else{
trace("No match was found");
}
Operation
So what REALLY happens? Heres the deal. switch sets the code to be used in the first expression of the comparison. This doesnt have to be a variable, it can just as well be a function - as long its something that resolves down to a single comparable value. When the switch statement is first encountered, this code is not initially run, but waits the comparison with the first case statement. When the first case is reached, the case expression (which can also be a function) is then thrown in a strict comparison operation, the switch expression being evaluated first, followed by the case and then the returned result (or the comparison) is given. If this is false, the case statements are skipped and the next case is evaluated. If true, all following case evaluations are stripped from the switch block and all the following statements are more or less combined to be a single executed block of code. Heres an example which can show you how this works:
switchVar = 0;
caseVar = 0;
switchCall = function(){
switchVar++;
trace("switch, switchVar: " + switchVar + ", caseVar: " + caseVar);
return switchVar;
}
caseCall = function(){
caseVar++;
trace("case, switchVar: " + switchVar + ", caseVar: " + caseVar)
return caseVar;
}
switch( switchCall() ){
case caseCall():
trace("first case");
case caseCall():
trace("second case");
case caseCall():
trace("third case");
default:
trace("default case");
}
Here you can see the switch call being run first, setting switchVar to 1. This is reflected in the case call which shows the new switchVar and sets its own caseVar. Each are returned and at the first case, the equality is true. Because there are no breaks, all case traces are called, but note the comparisons also stop (no more function calls). Once there is a true comparison, all further comparisons cease. To call the functions for each case, all youd need to do is change the initial set value of switchVar or caseVar to something different than the other. Then you can see each function call as its run for each case (though NOT the default case as it does not compare).
Here you need to be careful though. Be aware that switch (++myVar) is different than siwtch (myVar++) since in the instance of ++myVar, myVar will be incremented prior to the case check, while myVar++ will be incremented after.
[NOTE: The repeating switch condition evaluation is specific to Flash MX only, Flash MX 2004 fixes this and the switch is condition is only checked/set once and compared from there with all case comparisons]
The down and dirty of it all is that switch allows you to easily write out concise if equaity conditions in a simple alternative manner. Needed? No. Helpful? Sometimes. heh - I actually never use it, though, no doubt it can come in handy under the right circustances.
For more information, look up switch for Javascript - it should be the exact same thing and theres no doubt tons more documentation out there for it.