"Sputnik" help  
Sputnik Help
Switch ( <expression> )
{
Case <expressions>
{
statements
...
}
Case <expressions>
{
statements
...
}
CaseID <Identifier> <expressions>
{
statements
...
} Default
{
statements
...
}
}

Description

Conditionally run statements.

Parameters

Switch <expression>
An expression that returns a value. This value is then compared against the values of each case until a match is found.
Case <expressions>
CaseId <expression>
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 like

GotoCase Testy; // Goto a case that has a CaseId of this text
GotoCase 0; // Go to the case by this index number so first one is 0 second is 1
GotoNext; // Go to the next case ie instantly fall through
GotoPrev; // Go to the previous case ie fall upwards
GotoDefault; // Go to the "default" case
GotoStart; // Go to the very top of the Switch and start over again
GotoEnd; // Instantly drop out of the entire Switch

// Here are condition varients of the above
GotoEndIf
GotoEndUnless
GotoCaseIf
GotoCaseUnless
GotoNextIf
GotoNextUnless
GotoPrevIf
GotoPrevUnless
GotoDefaultIf
GotoDefaultUnless

(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

GotoStart;

inside a case to instantly jump to the very top of the Switch and start it over again.

You can place a

GotoEnd;

inside a case to instantly jump to the bottom of the Switch to exit it fully.

You can place a

GotoPrev;

inside a case to instantly jump to the default: previous statement if one exists.

You can place a

GotoNext;

inside a case to instantly jump to the default: next statement if one exists.

You can place a

GotoDefault;

inside a case to instantly jump to the default: statement if one exists.

You can place a

GotoCase 0;

inside a case to instantly jump to the case Id (by index so first case is 0 second is 1 and so on) you specify you change 0 to the id of the case you wish to jump to.

You can place a

GotoCase Fox;

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.

Note - When you use "Goto" 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).

The Macros:

@CaseState

This macro is set to true if the current the Case you are in was a true match (Even if you used continue to get into that case) otherwise it is false see example below.

Also it is recommended you copy the @CaseState macro immediately if you want to use or else it might get modified elsewhere.

These macros are to be used in the start/end blocks to check if all the sub-switch matches were true/false based on the macro name (see examples):

@CaseAll // True if all cases match
@CaseAny // True of any case matches
@CaseOne // True if only case matches
@CaseNone // True if no cases match

All conditional gotos work similar to an if statement (or unless statement) so for example:

GotoCaseIf($a == 10) Fox;

All the listed types above show which ones allow if/unless.

The Switch statement in Sputnik provides a concise and structured way to handle multiple cases based on the evaluation of an expression. It offers a clear and readable approach to branching based on the value of the expression.

Within the Switch statement, you can define multiple Case blocks, each with its own set of expressions to evaluate. The expression within each Case block is compared to the expression specified in the Switch statement. If there is a match, the statements within that Case block are executed. Only the first matching Case block is executed, and subsequent Case blocks are skipped.

You can use logical operators, comparison operators, and parentheses to create complex expressions for precise condition matching within each Case block. Additionally, you can assign identifiers to cases using the CaseId variant. These identifiers can be used later to navigate to specific cases using the provided GotoCase statements.

The Default block is optional and is executed only if none of the previous Case blocks match the expression. It serves as a fallback section to handle cases where none of the specified conditions are met.

It's important to note that without a Break statement at the end of a Case block, the code execution will fall through to the next Case block, allowing for cascading effects. However, if you want to prevent fall-through behavior, you should include a Break statement at the end of each Case block.

The start and end sections in Switch statements provide a mechanism for encapsulating a group of case statements within a larger Switch block. This feature allows for the execution of additional statements before and after the grouped cases, enhancing code organization and readability. See examples below.

The start and end system follows specific rules to enhance the functionality of Switch statements. When encountering a start block, it immediately executes its code. If a break statement is present within start, it behaves similarly to a regular case, terminating the Switch statement. However, if there's no break, Sputnik continues executing subsequent case statements within start until it reaches an explicit break or encounters the end block.

Upon encountering an end block, Sputnik executes its code. Like start, if there's a break within end, it ends the Switch statement. Notably, developers can include multiple statements within both start and end blocks, facilitating more complex logic structures. The cases inside a start/end block operate independently of the main Switch and can have their own default and case names etc.

This approach allows for nested Switch-like behavior within start and end blocks, enabling the handling of grouped cases and the execution of additional code before and after those cases. Overall, the start and end system in Sputnik enhances the expressiveness and flexibility of Switch statements, offering a powerful tool for structuring code effectively.

Switch statements can be nested within other Switch statements, providing hierarchical decision-making structures. This nesting capability allows you to handle complex scenarios and efficiently manage control flow based on multiple levels of conditions.

The Switch statement in Sputnik offers a powerful and structured approach for handling multiple cases and executing specific statements based on those cases. By utilizing the Switch statement, you can improve the readability, maintainability, and efficiency of your code when dealing with branching logic.

Related

Select Statement
Continue Statement
Redo Statement
Break Statement

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):

my $var = 1;
Switch ( $var )
{
    Case 1:
    Case 2:
    {
            say "Value is 1 or 2";
    }
    break;
    Case 3:
    {
            say "Value is 3";
    }
    break;
    Case 4, 7, 9:
    {
            say "Value is 4 or 7 or 9";
    }
    break;
    Case array(100, 200, 300, "moo"):
    {
            say "Value is either 100 or 200 or 300 or \"moo\"";
    }
    break;
    Case 7 rr 10:
    {
            say "Value is between 7 and 10";
    }
    break;
    Case 0x10 rr 0x20:
    {
            say "Value is a hex number between 0x10 and 0x20";
    }
    break;
    Case 'A' rr 'F':
    {
            say "Value is a char between A and F";
    }
    break;
    Case "test":
    {
            say "Value is \"test\"";
    }
    break;
    Case "test", "dog":
    {
            say "Value is \"test\" or \"dog\"";
    }
    break;
    Default:
    {
            say "No preceding case was true!";
    }
}
// PRINTS
// Value is 1 or 2

Its worth noting the Range Operator rr supports stepping using rs like so:

switch (6)
{
    case 5 rr 10 rs 2:
        say "Within 5 to 10";
        break;
    case 6:
        say "Is 6";
        break;
    default:
        say "Not found";
        break;
}
switch (8)
{
    case 5 rr 10 rs 2:
        say "Within 5 to 10";
        break;
    case 6:
        say "Is 6";
        break;
    default:
        say "Not found";
        break;
}
switch (9)
{
    case 5 rr 10 rs 2:
        say "Within 5 to 10";
        break;
    case 6:
        say "Is 6";
        break;
    default:
        say "Not found";
        break;
}
// Is 6
// Not found
// Within 5 to 10

This one uses the "GotoDefault" to instantly jump to the default statement:

my $a = "Cat"; 
Switch( $a )
{
    case "FoX":
        say "FoX";
        break;
    case "Cat":
        say "Cat";
        GotoDefault;
    Case "Dog":
        say "Dog";
        break;
    default:
        say "Default";
        break;
}
// PRINTS
// Cat
// Default

This one uses "GotoCase ID":

my $var = "Dog";
Switch ( $var )
{
    Case "Cat":
    {
        say "This is Case \"Cat\"";
        GotoCase Testy; // jump to Testy case so "Fox"
    }
    break;
    Case "Dog":
    {
        say "This is Case \"Dog\"";
        GotoPrev; // go to previous case so "Cat"
    }
    break;
    CaseID Testy "Fox": // Set an ID for a continue to jump to
    {
        say "This is Case \"Fox\"";
        GotoNext; // go to next case so "Cow"
    }
    break;
    Case "Cow":
    {
        say "This is Case \"Cow\"";
        GotoDefault; // jump to Default:
    }
    break;
    Default:
    {
        say "No preceding case was true!";
    }
    break;
}
// PRINTS
// This is Case "Dog"
// This is Case "Cat"
// This is Case "Fox"
// This is Case "Cow"
// No preceding case was true!

Example of @CaseState the @CaseState stores whether the current case was a true/false when it was checked even if you used any goto to get into the case (obviously if you used a goto then this will be false):

my $var = "Dog";
Switch ( $var )
{
    Case "Cat":
    {
        say "This is Case \"Cat\"";
        say "Was it true? @CaseState";
        GotoCase Testy; // jump to Testy case so "Fox"
    }
    break;
    Case "Dog":
    {
        say "This is Case \"Dog\"";
        say "Was it true? @CaseState";
        GotoPrev; // go to previous case so "Cat"
    }
    break;
    CaseID Testy "Fox": // Set an ID for a goto to jump to
    {
        say "This is Case \"Fox\"";
        say "Was it true? @CaseState";
        GotoNext; // go to next case so "Cow"
    }
    break;
    Case "Cow":
    {
        say "This is Case \"Cow\"";
        say "Was it true? @CaseState";
        GotoDefault; // jump to Default:
    }
    break;
    Default:
    {
        say "No preceding case was true!";
    }
    break;
}
// PRINTS
// This is Case "Dog"
// Was it true? true
// This is Case "Cat"
// Was it true? false
// This is Case "Fox"
// Was it true? false
// This is Case "Cow"
// Was it true? false
// No preceding case was true!

Notice only Dog was true? Since that is the only case that actually had a valid match instead of just a continue to get into it.

We can also use GotoStart to go back to the very top of the Switch itself.

my $a = "Cat"; 
Switch( $a )
{
    case "FoX":
        say "FoX";
        break;
    case "Cat":
        say "Cat";
        $a = "FoX";
        GotoStart;
}
// PRINTS
// Cat
// Fox

Nesting is possible such as:

my $a = "Cat"; 
Switch( $a )
{
    case "FoX":
        say "FoX";
        break;
    case "Cat":
        say "Cat";
        $a = "FoX";        
        my $b = "Moo"; 
        Switch( $b )
        {
            case "XD":
                say "XD";
                break;
            case "Moo":
                say "Moo";
                $a = "XD";
                GotoStart;
        }
        GotoStart;
}
// PRINTS
// Cat
// Moo
// XD
// FoX

We can also use GotoEnds to go immediately exit the Switch itself.

my $a = "Cat"; 
Switch( $a )
{
    case "Cat":
        say "Cat";
        GotoEnd;
        say "Purr";
        break;
}
say "Hello";
// PRINTS
// Cat

You can use the redo statement to do a specific case all over again such as:

my $a = "Cat"; 
my $i = 0;
Switch( $a )
{
    case "Cat":
        say "Cat ($i)";
        $i++;
        if ($i < 3)
            redo;
}
// PRINTS
// Cat (0)
// Cat (1)
// Cat (2)

We can also use start/end blocks within a Switch statement as shown here:

for (my $i = 0; $i < 6; $i++)
{
    switch ($i)
    {
        case 0: print "Zero "; break;
        case 2: print "Two "; break;
        case 4: print "Four "; break;
        start:
            print "[";
            case 1: print "One"; break;
            case 3: print "Three"; break;
            case 5: print "Five"; break;
        end:
            print "] ";
    }
}
// PRINTS
// Zero [One] Two [Three] Four [Five]

The provided code snippet features a loop that iterates from 0 to 5. Within each iteration, a Switch statement evaluates the current value of the loop variable $i. When $i is 0, 2, or 4, it prints "Zero ", "Two ", or "Four ", respectively. However, after case 4, a start section is encountered, printing an opening square bracket "[". Then, cases 1, 3, and 5 are evaluated, printing "One", "Three", and "Five" accordingly. Finally, an end section prints a closing square bracket "]" to complete the output. So, the output alternates between numbers and square brackets as it progresses through the loop.

Here is using multiple start/end statements:

for (my $i = 0; $i < 9; $i++)
{
    switch ($i)
    {
        case 0: print "Zero "; break;
        case 2: print "Two "; break;
        case 4: print "Four "; break;
        start:
            print "[";
            case 1: print "One"; break;
            case 3: print "Three"; break;
            case 5: print "Five"; break;
            default: print "N1";
        end:
            print "] ";
        start:
            print "(";
            case 6: print "Six"; break;
            case 7: print "Seven"; break;
            case 8: print "Eight"; break;
            default: print "N2";
        end:
            print ") ";
    }
}
// PRINTS
// Zero [One] (N2) Two [Three] (N2) Four [Five] (N2) [N1] (Six) [N1] (Seven) [N1] (Eight)

The start/end can also feature things to check just the same as a regular case which can help decide if they should execute or not:

for (my $i = 0; $i < 9; $i++)
{
    switch ($i)
    {
        case 0: print "Zero "; break;
        case 2: print "Two "; break;
        case 4: print "Four "; break;
        start 1, 3, 5:
            print "[";
            case 1: print "One"; break;
            case 3: print "Three"; break;
            case 5: print "Five"; break;
        end 1, 3, 5:
            print "] ";
        start 6, 7, 8:
            print "(";
            case 6: print "Six"; break;
            case 7: print "Seven"; break;
            case 8: print "Eight"; break;
        end 6, 7, 8:
            print ") ";
    }
}
// PRINTS
// Zero [One] Two [Three] Four [Five] (Six) (Seven) (Eight)

Same a above but showing ranges and arrays and regexes used for the start/end:

for (my $i = 0; $i < 9; $i++)
{
    switch ($i)
    {
        case 0: print "Zero "; break;
        case 2: print "Two "; break;
        case 4: print "Four "; break;
        start ([1, 3, 5]):
            print "[";
            case 1: print "One"; break;
            case 3: print "Three"; break;
            case 5: print "Five"; break;
        end array(1, 3, 5):
            print "] ";
        start 6 rr 8:
            print "(";
            case 6: print "Six"; break;
            case 7: print "Seven"; break;
            case 8: print "Eight"; break;
        end m/[6|7|8]/:
            print ") ";
    }
}
// PRINTS
// Zero [One] Two [Three] Four [Five] (Six) (Seven) (Eight)

This time there are multiple start/ends each one having its own set of conditions:

for (my $i = 0; $i < 6; $i++)
{
    switch ($i)
    {
        start 0, 2, 4:
            print "(";
            case 0: print "Zero"; break;
            case 2: print "Two"; break;
            case 4: print "Four"; break;
        end 0, 2, 4:
            print ") ";
        start 1, 3, 5:
            print "[";
            case 1: print "One"; break;
            case 3: print "Three"; break;
            case 5: print "Five"; break;
        end 1, 3, 5:
            print "] "; break;
    }
}
// PRINTS
// (Zero) [One] (Two) [Three] (Four) [Five]

Here is a better way to do the above:

for (my $i = 0; $i < 6; $i++)
{
    switch ($i)
    {
        start @CaseAny:
            print "(";
            case 0: print "Zero"; break;
            case 2: print "Two"; break;
            case 4: print "Four"; break;
        end @CaseAny:
            print ") ";
        start @CaseAny:
            print "[";
            case 1: print "One"; break;
            case 3: print "Three"; break;
            case 5: print "Five"; break;
        end @CaseAny:
            print "] ";
    }
}
// PRINTS
// (Zero) [One] (Two) [Three] (Four) [Five]
Notice the @CaseAny macro? This will be set to true if any of the cases inside a start/end block are true there is also @CaseAll and @CaseOne and @CaseNone (See remarks).

These patterns demonstrates a special feature in Sputnik programming language, where start and end sections can be utilized within a Switch statement. This feature allows for conditional execution of code blocks before and after a series of case statements, enhancing the flexibility and readability of the code. By encapsulating certain logic within start and end sections, developers can neatly organize related operations, improving code maintainability and reducing the likelihood of errors. This capability is particularly useful when dealing with complex Switch cases where additional processing needs to be performed before or after evaluating specific cases. Overall, the start and end sections offer a convenient way to structure code within Switch statements, enhancing the expressiveness and clarity of Sputnik scripts.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org