Usage
Syntax: switch (testvariable) { caselist; }
Explanation
Switch is a unique sort of loop that is often used in place of very long if cascades. Possible values of testvariable are enumerated in the case list, so that if testvariable has the value listed as a case, the code following that case statement, up to the first break, is executed.
If no break is found between one case statement and the next, then the following case statement is also executed. This is called case fall-through, and there is controversy surrounding whether or not to use it.
The default statement is usually the last statement in a switch case list. It is executed if no other value in the list is matched, or if the preceding case statement is executed and lacks a break.
Example
if (myInteger <= 10) { switch (myInteger) { case 1: echo("my integer's value is one"); break; case 2: case 3: case 5: case 7: echo("my integer's value is prime!"); break; default: echo("my integer's value is boring. :("); } }
Categories
CategoryLanguageConstruct
There are no comments on this page. [Add comment]