Switch
(→Remarks) |
(→Example) |
||
Line 197: | Line 197: | ||
break; | break; | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of @CaseState the @CaseState stores whether the current case was a true/false when it was checked even if you used goto to get into the case. | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Define some variables | ||
+ | $a = 100; | ||
+ | $b = 200; | ||
+ | $c = 50; | ||
+ | // Create the select | ||
+ | Select | ||
+ | { | ||
+ | // True if $a is 100 (which is true) | ||
+ | case $a == 100: | ||
+ | // Print case state | ||
+ | // will be TRUE since we just got into | ||
+ | // this case by a TRUE match on $a is 100 | ||
+ | say "Cat : @CaseState"; | ||
+ | // Jump to case Dog | ||
+ | goto _caseDog; | ||
+ | break; | ||
+ | caseId Dog $b == 200: // True if $b is 200 (which is true) | ||
+ | // Print case state | ||
+ | // will be TRUE since $b is 200 | ||
+ | say "Dog : @CaseState"; | ||
+ | // Jump to case FoX | ||
+ | goto _caseFoX; | ||
+ | break; | ||
+ | caseId FoX $c == 300: | ||
+ | // Print case state | ||
+ | // will be FALSE since $c is NOT 50 | ||
+ | say "FoX : @CaseState"; | ||
+ | break; | ||
+ | } | ||
+ | // Prints | ||
+ | // Cat : true | ||
+ | // Dog : true | ||
+ | // FoX : false | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Revision as of 16:11, 10 August 2014
Contents |
Select...Case...Default
Description
Conditionally run statements.
Switch ( <expression> ) { Case <expressions> { statement1 ... } Case <expressions> { statement2 ... } CaseID <Identifier> <expressions> { statement2 ... } Default { statementN ... } }
Parameters
Switch <expression>
Any vaid expression to used and compared with the Case <expressions>
Case <expressions>
Optional; You can use none, one or more.
If the expression matches the Switch <expression> the following statements up to the next Case or Default are executed. If more than one of the Case statements are true, only the first one is executed.
If you type CaseID instead of Case you can assign an ID to it like CaseID Testy: then you can jump to that case using goto _caseTesty;
(You can separate expressions to check using a , )
Note - If a Case is lacking a Break then the code will fall through into the next case and execute that and fall through again if theres no break.
Default
Optional; You can use one or none.
If none of the cases are true the code in Default will be executed.
You dont need a Break in the Default.
You can place the defauly anywhere in the statement.
Remarks
Switch statements may be nested.
Strings are case sensitive when used in a case.
You can place a goto _default; inside a case to instantly jump to the default: statement if one exists (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).
You can place a goto _case0; inside a case to instantly jump to the case ID you specify you change 0 to the id of the case you wish to jump to. (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).
You can place a goto _caseFox; inside a case to instantly jump to the case with the name you specify you change Fox to the Name of the case you wish to jump to. (This uses a hardcoded jump and does not reply on Sputniks goto at all so it should be be a lot faster).
Note - When you use goto _case stuff it will instantly begin executing code in that case it will NOT check if the case is a match for the object you are checking with the Switch (A warning will be given if the case/default you want to jump to does not exist).
@CaseState
This macro is set to TRUE if the current the Case you are in was a TRUE match otherwise it is false see example below.
Example
Heres an example with all breaks in proper place (Break statement is needed to tell the code to stop or else it wil fall through into the next case that may be what you want though)
$var = 1; Switch ( $var ) { Case 1: Case 2: { println("Value is 1 or 2"); } break; Case 3: { println("Value is 3"); } break; Case 4, 7, 9: { println("Value is 4 or 7 or 9"); } break; Case array(100, 200, 300, "moo"): { println("Value is either 100 or 200 or 300 or \"moo\""); } break; Case 7..10: { println("Value is between 7 and 10"); } break; Case 0x10..0x20: { println("Value is a hex number between 0x10 and 0x20"); } break; Case 'A'..'F': { println("Value is a char between A and F"); } break; Case "test": { println("Value is \"test\""); } break; Case "test", "dog": { println("Value is \"test\" or \"dog\""); } break; Default: { println("No preceding case was true!"); } }
This one uses the goto _default; to instantly jump to the default statement
$a = "Cat"; Switch( $a ) { case "FoX": say "FoX"; break; case "Cat": say "Cat"; goto _default; break; Case "Dog": say "Dog"; break; default: say "Default"; break; }
This one uses goto _caseID
$var = "Dog"; Switch ( $var ) { Case "Cat": { println('This is Case "Cat"'); goto _caseTesty; # jump to Testy case so "Fox" } break; Case "Dog": { println('This is Case "Dog"'); goto _casePrev; # go to previous case so "Cat" } break; CaseID Testy "Fox": # Set an ID for a goto to jump to { println('This is Case "Fox"'); goto _caseNext; # go to next case so "Cow" } break; Case "Cow": { println('This is Case "Cow"'); goto _default; # jump to Default: } break; Default: { println("No preceding case was true!"); } break; }
Example of @CaseState the @CaseState stores whether the current case was a true/false when it was checked even if you used goto to get into the case.
// Define some variables $a = 100; $b = 200; $c = 50; // Create the select Select { // True if $a is 100 (which is true) case $a == 100: // Print case state // will be TRUE since we just got into // this case by a TRUE match on $a is 100 say "Cat : @CaseState"; // Jump to case Dog goto _caseDog; break; caseId Dog $b == 200: // True if $b is 200 (which is true) // Print case state // will be TRUE since $b is 200 say "Dog : @CaseState"; // Jump to case FoX goto _caseFoX; break; caseId FoX $c == 300: // Print case state // will be FALSE since $c is NOT 50 say "FoX : @CaseState"; break; } // Prints // Cat : true // Dog : true // FoX : false