PDA

View Full Version : Name of a switch array


FEK315
03-16-2011, 04:31 PM
In this Switch Array:

letter = 'a' ;
switch (letter) {
case 'a':
trace ("A"); break ;
case 'b':
trace ("B"); break ;
case 'c':
trace ("C"); break ;
default:
trace ("The letter is not a, b or c") ;

Is 'a', the name of the Switch Array?
Thank you :eek:

xxneon
03-16-2011, 04:36 PM
switch isn't an array.. its another form of a conditional statement.. same as using if ... else if ... else if ...

you give the switch the value you want to compare.. and then depending on the value it will run the code associated to the specific case and keep executing code till it either hits a break; or the end of the switch.

FEK315
03-16-2011, 04:47 PM
Is it faster then an Array?

In this example is 'a' the Name of the Switch?

Thank you for explaining the difference, I was under the impression that Arrays were kind of replaced by the Switch.;)

xxneon
03-16-2011, 04:55 PM
no they are two completely different elements of actionscript..

a switch is in no way an array..

an array is an object that can store data at numeric indexes.. so you can hold more then one of any type of data.

a switch is not an object.. it doesn't store any type of data..

a switch is similar to a if statement. but slightly different.. with an if statement you usually compare something to see if the statement is true or false, and run code accordingly.. with a switch statement you can compare the current value of something and then based on that value you can have the 'cases' structured to have different code run based on the value..

switch statements are said the be faster then doing multiple if (or if... else) statements because its able to jump right down to the case that applies instead of having to compare each if statement till if finds the one that fits the situation..

so really you shouldn't be comparing switch with an array at all.. they really don't have anything in common.

FEK315
03-16-2011, 05:10 PM
This is really helpful and clears a lot up.
Thank you!!!

marlopax
03-17-2011, 03:44 PM
There is some little difference between my thoughts for if/else statement with switch.

var a = "A";
var b = "B";

switch (a == b) {
case true :trace("SWITCH : "+b);break;
case false :trace("SWITCH : "+a);break;
}

switch (a != b) {
case true :trace("SWITCH : "+b);break;
case false :trace("SWITCH : "+a);break;
}
if (a == b) {
trace("IF : "+b);break;
} else {
trace("IF : "+a);break;
}

if (a != b) {
trace("IF : "+b);break;
} else {
trace("IF : "+a);break;
}


By this example I am trying to show that you can compare in both cases. and if we use the break; both will perform almost same.

Please don't mind xxneon. I want your comment on this if I am wrong.

FEK315 : There is no way to name a Switch Statements or any Statements in any languages because it throws the conditions of Objects/Elements.


Regards



marlopax

xxneon
03-17-2011, 04:05 PM
@marlopax:

well break; is not used for if statements.. it really only comes into play with terminating for loops and ending code for a case statement so that the switch doesn't accidentally run code for the next case statement..

and I know that you can use switch to evaluate true false.. but I guess I figured that performance wise .. you don't gain much since its basically only evaluating to two possible solutions.. same as an if..else.

I mean the shorthand if statement is probably doing the same thing as a true/false switch and its less lines..

(a == b) ? trace(a) : trace(b);

marlopax
03-17-2011, 04:34 PM
You are right xxneon, but what makes a real difference in if/else vs switch ?
and also of that (a == b) ? trace(a) : trace(b);

What I think you can state n numbers of cases in a switch whereas it could a readability problem in if/else.


Regards


marlopax

FEK315
03-17-2011, 05:22 PM
:)

Super COOL!

So then is it the function that the switch is in I name?

Thank you

xxneon
03-17-2011, 05:25 PM
switches execute faster then doing multiple if...else if...else if.. this is the biggest thing.. and yes its easier to read then a bunch of if...else statements..

the shorthand if statement comes in handy when you know that you only have two possible situations.. either this or that .. its not as easy to read a single if..else statement.. but once you use it enough you can understand it.

one thing if statements have over switch statements are the fact that if statements can do complex comparisons.. and switches are mainly only meant to be used for simple comparisons since your basically giving it a single value and then it compares it with all the possible cases to see which case to execute.

you can't compare multiple values at the same time with one switch statement.