"Sputnik" help  
Sputnik Help
Select
{
Case <expressions>
{
statement1
...
}
Case <expressions>
{
statement2
...
}
CaseID <Identifier> <expressions>
{
statement2
...
}
Default
{
statementN
...
}
}

Description

Conditionally run statements.

Parameters

Case <expression>
CaseId <expression>
Optional; You can use none, one or more.

If the case is true it will be 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 Select and start over again
GotoEnd; // Instantly drop out of the entire Select

// 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 default anywhere in the statement.

Remarks

Go read the remarks on Switch since it's case labels apply here too.

Select statements may be nested.

Strings are case sensitive when used in a case.

A select acts very much like an If statement however it lets you fallthrough if thats useful to you.

The Select statement in Sputnik provides a powerful mechanism for conditionally executing statements based on the evaluation of expressions. It offers a flexible and structured way to handle multiple cases and choose the appropriate actions.

Within the Select statement, you can define multiple Case blocks, each with its own set of expressions to evaluate. The expressions within a Case block are checked, and if any of them evaluate to true, the statements within that Case block are executed. You can use logical operators, comparison operators, and parentheses to create complex expressions for precise condition matching.

The CaseId variant of the Case block allows you to assign an identifier to the case. This identifier can be used later to jump to a specific case using GotoCase statements. This feature enables dynamic control flow within the Select statement, allowing you to navigate to different cases based on program logic.

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

It's important to note that without a Break statement, the code execution will fall through to the next case. 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 Select statements provide a mechanism for encapsulating a group of case statements within a larger Select 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 Select 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 Select 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 Select 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 Select and can have their own default and case names etc.

This approach allows for nested Select-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 Select statements, offering a powerful tool for structuring code effectively.

Select statements can be nested within other Select statements, allowing for hierarchical decision-making structures. This nesting capability provides flexibility in designing complex control flow scenarios.

The Select statement in Sputnik offers a robust solution for handling multiple conditions and executing specific statements based on those conditions. It provides an organized and readable approach to managing complex decision-making logic in your code. By utilizing the Select statement, you can streamline your code's execution paths, improve maintainability, and make it easier to handle various cases efficiently.

Related

Switch 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 = 4;
Select
{
    Case $var == 1:
    Case $var == 2:
    {
            say "Value is 1 or 2";
    }
    break;
    Case $var == 3, $var == 4:
    {
            say "Value is 3 or 4";
    }
    break;
    Case $var != 5:
    {
            say "Value is not 5";
    }
    break;
    Case $var == "test":
    {
            say "Value is \"test\"";
    }
    break;
    Default:
    {
            say "No preceding case was true!";
    }
}
// PRINTS
// Value is 3 or 4

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

// Define some variables
my $a = 100;
my $b = 200;
my $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
        GotoCase Dog;
        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
        GotoCase FoX;
        break;
    caseId FoX $c == 300:
        // Print case state
        // will be FALSE since $c is NOT 300
        say "FoX : @CaseState";
        break;
}
// Prints
// Cat : true
// Dog : true
// FoX : false

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

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

In this code snippet, a loop iterates through values of $i from 0 to 5 inclusively. Within each iteration, a select block is used to evaluate conditions for each case, executing the corresponding code block based on the condition's result. The distinctive feature is the utilization of start and end blocks, which act as delimiters for a subsection of cases. Within the start block, "[" is printed, and subsequent cases within this block handle values of $i equal to 1, 3, and 5, printing "One", "Three", and "Five" respectively. After these cases, the end block is reached, printing "] ". This mechanism allows for the execution of specific code sections based on conditions, providing a structured and concise approach to handle different scenarios within the loop.

for (my $i = 0; $i < 9; $i++)
{
    select
    {
        case $i == 0: print "Zero "; break;
        case $i == 2: print "Two "; break;
        case $i == 4: print "Four "; break;
        start:
            print "[";
            case $i == 1: print "One"; break;
            case $i == 3: print "Three"; break;
            case $i == 5: print "Five"; break;
            default: print "N1";
        end:
            print "] ";
        start:
            print "(";
            case $i == 6: print "Six"; break;
            case $i == 7: print "Seven"; break;
            case $i == 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)

This pattern demonstrates a special feature in Sputnik programming language, where start and end sections can be utilized within a Select 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 Select 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 Select statements, enhancing the expressiveness and clarity of Sputnik scripts.


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