FANDOM


PromptEdit

DescriptionEdit

Checks a value for specific conditions and executes code when found.

SyntaxEdit

Switch(param1) {
 case "sub_param1" :
   <executable code>
   break;
 case "sub_param2" :
   <executable code>
   break;
 case "sub_param3" :
   <executable code>
   break;
 ...
 default:
   <executable code>
}

ExampleEdit

Switch(Dog) {
 case "Bulldog" :
   alert("GROWL");
   break;
 case "Chihuahua" :
   alert("Bark!");
   alert("Bark bark!");
   break;
 case "Basset Hound":
 case "Beagle":
   alert("Sniff sniff");
   break;
 default:
   alert("Drool");
}



NotesEdit

This is like a series and ifElse statements but cleaner to write. Adding two cases together without a break lumps them into an "or." In the example above, the third case is asking if the dog is a Basset Hound or a Beagle. Default cases do not need a break.