"Sputnik" help  
Sputnik Help
Do
{
statements
...
} Forever (<condition>);

Description

Unlike for, while and until loops, which test the loop condition at the top of the loop, the do...forever loop checks its condition at the bottom of the loop.

A do...forever loop is similar to a forever loop, except that a do...forever loop is guaranteed to execute at least one time.

This creates a loop that executes a block of code at least once and then continues indefinitely or until explicitly broken.

Parameters

<condition>

The optional condition is evaluated just like an If statement. If it is true, the body of the loop is executed initially. If it is false, the body of the loop does not execute (it is guaranteed to execute the body once regardless if the condition is true or not) and flow of control jumps to the next statement just after the for loop. If the condition is not specified or evaluates to true, the loop will continue indefinitely. If the condition is false, the loop will be skipped. The condition is only checked once and is ignored for all iterations past that point.

Remarks

Do...Forever statements may be nested.

The Do...Forever loop is a control structure that guarantees the execution of the code block at least once. It provides a mechanism to create an infinite loop that either ignores the condition after the first check or loops indefinitely without any condition.

If a <condition> is provided, it will be evaluated after the first iteration of the loop. If the condition evaluates to true, the loop will continue executing the code block. However, unlike a typical loop, the condition will not be re-evaluated in subsequent iterations. This means that the loop will continue indefinitely, regardless of the condition's value.

If the <condition> is not specified (i.e., an empty parentheses ()), the loop will continue indefinitely without any condition to break the loop. In such cases, it's important to ensure there is an appropriate mechanism to break out of the loop, such as using a break statement when a specific condition is met.

Related

Loop Statements
Continue Statement
Redo Statement
Break Statement

Example

Basic Example:

my $a = 10;

do
{
    say "Cat! $a";
    $a++;
    if ($a == 20)
        break;
}
forever($a == 11);

// PRINTS
// Cat! 10
// Cat! 11
// Cat! 12
// Cat! 13
// Cat! 14
// Cat! 15
// Cat! 16
// Cat! 17
// Cat! 18
// Cat! 19

As we see here the code always executes once:

my $a = 5;

do
{
    say "Cat! $a";
    $a++;
    if ($a == 7)
        break;
}
forever($a == 10);

// PRINTS
// Cat! 5

Loop forever with no condition:

my $a = 5;

do
{
    say "Cat! $a";
    $a++;
    if ($a == 20)
        break;
}
forever();

// PRINTS
// Cat! 5
// Cat! 6
// Cat! 7
// Cat! 8
// Cat! 9
// Cat! 10
// Cat! 11
// Cat! 12
// Cat! 13
// Cat! 14
// Cat! 15
// Cat! 16
// Cat! 17
// Cat! 18
// Cat! 19

 


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