What does it mean?

hi guys,

switch (myVar) { 
     case 1: 
     case 2: 
          _level0.colorSelector.gotoAndStop(2); 
          break; 
     case 3: 
     case 8: 
          _level0.colorSelector.gotoAndPlay(myVar); 
          break; 
     default: 
          _level0.colorSelector.gotoAndStop(1); 
} 

can anyone tells me what does it mean?
thanx :slight_smile:

switch statements are basically a shorthand to a long if/else block. What it does is checks the value in the switch parens with each ‘case’ and if true, executes the code following the colon. What the break does is breaks out of the switch so none of the other cases are run again afterwards.

So what that does is checks myVar to be either 1, 2, 3, or 8 and performs the resulting commands, though 1 and 3 have nothing so they do noth … well we’ll get to that in the next paragraph. default is for when none of the case statements are correct. So if myVar is neither 1,2,3, or 8 then the default ‘case’ is run.

There are some oddities with a switch/case block which may seem counter-intuitive though add to its functionality. For instance, if there is a correct case to match the switch variable, that following code will be run, BUT so will all the code after that whether or not the variable matches the case statement until trhe block ends or a break is reached. So in this case, if myVar is 1, anything in case 1: will be run AND all the code past that point (no matter what the case) until a break is reached which is at the end of 2. So really, having those empty case lines doesnt mean that those cases do nothing, they are just a type of ‘or’ saying if myVar is 1 or 2 then _level0.colorSelector.gotoAndStop(2); And if myVar is 3 or 8 _level0.colorSelector.gotoAndPlay(myVar); <- WHICH will be either 3 or 8 depending (where as 1 or 2 will always go to 2).

in terms of an if/else statement, that code would look like this:


if (myVar == 1 || myVar == 2) {
	_level0.colorSelector.gotoAndStop(2); 
}else if (myVar == 3 || myVar == 8){
	_level0.colorSelector.gotoAndPlay(myVar);
}else{
	_level0.colorSelector.gotoAndStop(1);
}

hey senocular,
many thanx for you nice explaniation… :stuck_out_tongue:
but i want to ask about [color=red]break[/color], which is breaks out of the switch… so is mean that nothing will happens to other statments or it’ll continue to check other cases.

i said that beacuse i know break breaks all others code after it.
is it right :q:

thanx

can you give a nice example and explanation of switch on tricks of the trade thread?
plz? :wink:

yeah the break breaks out of the code block. This is the same for whilfe and for loops too. If you want to stop looping and break out of the loop block, youd use the break command. Thats what its used for here too. If a case resolves to be true, if will run all the code which follows. That is unles a break is found which stops the block (switch block) right there and skips everything down to the end of the } ending the statement.

:stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue:

thanx man :flower: