Core Function Goto Label
From Sputnik Wiki
(Difference between revisions)
(→Example) |
|||
| Line 32: | Line 32: | ||
Below: | Below: | ||
println( "Hello Below" ); | println( "Hello Below" ); | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | Goto can be pretty crazy in where it can jump you to for example: | ||
| + | |||
| + | <syntaxhighlight lang="sputnik"> | ||
| + | { | ||
| + | if( "Cat" == "Dog") | ||
| + | { | ||
| + | if( "Cat" == "Dog") | ||
| + | { | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | while(false) | ||
| + | { | ||
| + | switch(100) | ||
| + | { | ||
| + | case 1: | ||
| + | { | ||
| + | } | ||
| + | break; | ||
| + | case 2: | ||
| + | { | ||
| + | fox: | ||
| + | say "FOX!!!!!!!!!!!!!!!!!"; | ||
| + | goto finish; | ||
| + | } | ||
| + | break; | ||
| + | case 3: | ||
| + | { | ||
| + | while(false) | ||
| + | { | ||
| + | foreach("lol" as $c) | ||
| + | { | ||
| + | for($i = 0; $i < 10; $i++) | ||
| + | { | ||
| + | for( 0 .. 30 as $k ) | ||
| + | { | ||
| + | cat: | ||
| + | say "CAT!!!!"; | ||
| + | goto fox; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | break; | ||
| + | case 4: | ||
| + | { | ||
| + | } | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | say "Begin"; | ||
| + | goto test; | ||
| + | |||
| + | if(1==2) | ||
| + | { | ||
| + | say "Test"; | ||
| + | test: | ||
| + | say "Hello"; | ||
| + | } | ||
| + | |||
| + | say "End"; | ||
| + | goto cat; | ||
| + | finish: | ||
| + | say "OK I'm done"; | ||
| + | // Prints: | ||
| + | // Begin | ||
| + | // Hello | ||
| + | // End | ||
| + | // CAT!!!! | ||
| + | // FOX!!!!!!!!!!!!!!!!! | ||
| + | // OK I'm done | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] | ||
Revision as of 11:46, 27 August 2013
Goto Label; & Label:
Description
Jump to another section of the script
Remarks
This is not a universal *jump to anything you can see* it can only jump through local scopes and what not.
Think of it as a kind of multilayer break statement.
Note - People often avoid using "goto" statements in languages because they tend to be slow and a very poor way of solving a problem yet its here if you want it.
If a label cannot be found an exception will occur.
Example
goto Begin; Above: println( "Hello Above" ); goto Below; Begin: println( "Hello" ); goto Above; Below: println( "Hello Below" );
Goto can be pretty crazy in where it can jump you to for example:
{ if( "Cat" == "Dog") { if( "Cat" == "Dog") { } else { while(false) { switch(100) { case 1: { } break; case 2: { fox: say "FOX!!!!!!!!!!!!!!!!!"; goto finish; } break; case 3: { while(false) { foreach("lol" as $c) { for($i = 0; $i < 10; $i++) { for( 0 .. 30 as $k ) { cat: say "CAT!!!!"; goto fox; } } } } } break; case 4: { } break; } } } } } say "Begin"; goto test; if(1==2) { say "Test"; test: say "Hello"; } say "End"; goto cat; finish: say "OK I'm done"; // Prints: // Begin // Hello // End // CAT!!!! // FOX!!!!!!!!!!!!!!!!! // OK I'm done