"Sputnik" help  
Sputnik Help
name:
{ statements
...
} name:;

Description

This is similar to a Scope statement but it has a name and can be used with Continue and Break.

Sputnik uses this because there is no Goto keyword anymore in Sputnik this is because Goto was far too messy and only caused problems where as this does not and works nicely.

A Break statement will make this statement terminate.

A Continue statement will make this statement start over again like the first time it was run.

Parameters

Name The name of this named block statement.

Remarks

The named block statement in Sputnik provides a convenient and structured way to define blocks of code with a specific name. It offers similar functionality to the scope statement but with added features that make it compatible with the continue and break statements.

With the named block statement, you can create blocks of code labeled with a name, allowing you to easily reference and control the execution flow within those blocks. The break statement can be used to terminate the named block, while the continue statement can restart the execution of the block as if it were being run for the first time.

This construct is particularly useful in situations where you want to perform conditional jumps within your code without resorting to traditional goto statements. The named block statement provides a cleaner and more structured alternative, ensuring that your code remains readable and maintainable.

You can also use the named block statement as an anchor point for break and continue statements by omitting the braces and using a single semicolon (;) after the block name. This allows you to jump to or from the named block, enhancing code control and flexibility.

By leveraging the power of named block statements, you can achieve more structured and readable code that effectively manages control flow within different blocks. It combines the benefits of named blocks with the flexibility of continue and break statements, enabling you to handle complex scenarios and improve code clarity.

Related

Continue Statement
Break Statement

Example

Basic example using the Break statement this will cause the code to drop out at the } token:

OuterLoop:
{
    say "Step one";
    break OuterLoop;
    say "Step two";
}
say "Step three";
// PRINTS
// Step one
// Step three

Basic example using the Continue statement this will cause the block to start again:

my $value = 0;

OuterLoop:
{
    say "Value is: $value";
    $value++;
    if ($value < 3)
        continue OuterLoop;
    say "Finished!";
}
say "All done.";
// PRINTS
// Value is: 0
// Value is: 1
// Value is: 2
// Finished!
// All done.
So Continue will start the statement over again and Break will drop out of the statement entirely.

Lets take it to the extreme:

my $value = 0;
say "Starting...";
YetAnotherLoop:
{
    OuterLoop:
    {
        if ($value < 5 && $value > 1)
        {
            say "Value is ($value) lower than five but higher one adding one";
            $value++;
            continue OuterLoop;
        }
        if ($value == 1)
        {
            say "Value is one adding one";
            $value++;
            break OuterLoop;
        }
        InnerLoop:
        {
            if ($value == 5)
            {
                say "All done!";
                break YetAnotherLoop;
            }
            if ($value == 0)
            {
                say "Value is zero adding one";
                $value++;
                continue OuterLoop;
            }
        }
    }
    if ($value == 2)
    {
        say "Value is two";
        continue YetAnotherLoop;
    }
}
say "Finished";
// PRINTS
// Starting...
// Value is zero adding one
// Value is one adding one
// Value is two
// Value is (2) lower than five but higher one adding one
// Value is (3) lower than five but higher one adding one
// Value is (4) lower than five but higher one adding one
// All done!
// Finished

As you can see it can be fairly complex.

As a bonus you can use Continue statement to jump to another named block that is not touching as long as its within the same scope or higher scope example:

say "Begin";
continue dog;
something:
{
    say "Something";
    continue cat;
}
test:
{
    say "test";
    continue fish;
    kitten:
    {
        say "kitten";
        continue finish;
    }
    fish:
    {
        say "fish";
        continue kitten;
    }
}
cat:
{
    say "cat";
    continue test;
}

dog:
{
    say "Dog";
    continue something;
}
finish:
{
    say "done!";
}
// PRINTS
// Begin
// Dog
// Something
// cat
// test
// fish
// kitten
// done!

So as you can see it can jump to same scope or higher scopes but not jump to inner scopes.

Basically this allows a "goto" type code but in a way that works good without sacrificing speed for Sputnik.

To jump past a named block just use the Break statemennt like so:

say "begin";

test:
{
    say "test";
    break cat;
}

cat:
{
    say "cat";
}

say "end";
// PRINTS
// begin
// test
// end

You can also omit the { } brace and just use a single ; if you want to use the named block as a kind of anchor to break/continue to/from like so:

say "begin";

test:
{
    say "test";
    break cat;
}

cat:;

say "end";
// PRINTS
// begin
// test
// end

 


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