"Sputnik" help  
Sputnik Help
continue;
continue <arg>;
continueIf(<cond>);
continueIf(<cond>) <arg>;
continueUnless(<cond>);
continueUnless(<cond>) <arg>;

Description

The continue statement is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

Parameters

<arg> Optional; This can either be numeric to signal how many continue (break out then continue) to achieve or a string/identifier to signal what named statement to continue (break out then continue) to.. Default: 1.

Remarks

The continue statement in Sputnik provides a way to skip the remaining code within the current iteration of a loop and proceed to the condition evaluation and the start of the next iteration. It is commonly used to control the flow of execution within loops.

Similar to the break statement, the continue statement also supports an optional argument: continue <arg>. This argument can be either numeric or a string/identifier. When a numeric argument is provided, it specifies how many levels of loops to break out from before continuing to the next iteration. On the other hand, a string/identifier argument refers to a named statement to break out from and continue execution.

Additionally, Sputnik offers two useful variations of the continue statement: ContinueIf(<condition>) and ContinueUnless(<condition>). These variations allow you to conditionally continue to the next iteration of a loop based on the specified condition. The ContinueIf(<condition>) statement continues the loop only if the condition evaluates to true, while the ContinueUnless(<condition>) statement continues if the condition evaluates to false.

By using the continue statement, you can fine-tune the behavior of your loops, skipping specific iterations based on runtime conditions or breaking out of nested loops when necessary. This flexibility enables you to create more efficient and controlled loop structures in your code.

In summary, the continue statement, along with its variations ContinueIf(<condition>) and ContinueUnless(<condition>), empowers you to manage loop iterations dynamically and selectively based on conditions, improving the control flow and efficiency of your code.

Related

Loop Statements
Try Statement
Switch Statement
Select Statement
Redo Statement
Break Statement
Named Block Statement

Example

Using it on a Foreach statement:

my $arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach (my $num in $arr)
{
    if (!($num % 2)) // skip even members
        continue;
    say "Number is: $num";
}
// PRINTS
// Number is: 1
// Number is: 3
// Number is: 5
// Number is: 7
// Number is: 9

Using the optional argument:

my $i = 0;
while ($i++ < 5)
{
    say "Outer";
    while (1)
    {
        say "Middle";
        while (1)
        {
            say "Inner";
            continue 3;
        }
        say "This never gets output.";
    }
    say "Neither does this.";
}
// PRINTS
// Outer
// Middle
// Inner
// Outer
// Middle
// Inner
// Outer
// Middle
// Inner
// Outer
// Middle
// Inner

Another example:

for (my $i = 0; $i < 3; $i++)
{
    say "Start Of I loop";
    for ($j = 0; true; $j++)
    {   
        if ($j >= 2)
            continue 2; // This "continue" applies to the "$i" loop
        say "I : $i J : $j";
    }
    say "End";
}
// PRINTS
// Start Of I loop
// I : 0 J : 0
// I : 0 J : 1
// Start Of I loop
// I : 1 J : 0
// I : 1 J : 1
// Start Of I loop
// I : 2 J : 0
// I : 2 J : 1

Yet another example:

my $primes = SayPrimesBetween(10, 30);
printr $primes;
function SayPrimesBetween($x, $y)
{
    my $primes = [];
    for(my $i = $x; $i <= $y; $i++)
    {
        for(my $j = 2; $j < $i; $j++)
            if($i % $j == 0)
                continue 2;
        $primes[] = $i;
    }
    return $primes;
}
// PRINTS
// Array
// (
//     [0] => 11
//     [1] => 13
//     [2] => 17
//     [3] => 19
//     [4] => 23
//     [5] => 29
// )

You can use If/Unless with a continue like so:

my $arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach (my $num in $arr)
{
    continueIf($num % 2); // skip even members
    say "Number is: $num";
}
// PRINTS
// Number is: 2
// Number is: 4
// Number is: 6
// Number is: 8
// Number is: 10

my $arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach (my $num in $arr)
{
    continueUnless(!($num % 2)); // skip even members
    say "Number is: $num";
}
// PRINTS
// Number is: 2
// Number is: 4
// Number is: 6
// Number is: 8
// Number is: 10

 


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