"Sputnik" help  
Sputnik Help
break;
break <arg>;
breakIf(<cond>);
breakIf(<cond>) <arg>;
breakUnless(<cond>);
breakUnless(<cond>) <arg>;

Description

The break state,emt ends execution of the current for, foreach, while, do-while, do-until or switch/select structure.

Parameters

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

Remarks

The break statement in Sputnik allows you to prematurely terminate the execution of the current for, foreach, while, do-while, do-until, or switch/select structure. When encountered, the break statement immediately exits the loop or switch and continues executing the code following the loop or switch.

By default, the break statement ends the immediate enclosing loop or switch. However, you can provide an optional argument to specify the number of nested loops or switches to break out of, or you can provide a string/identifier to signal a named statement to break to.

In the provided examples, you can see the usage of the break statement in different scenarios. In the first example, the while loop runs indefinitely, but the break statement is encountered after the first iteration, preventing the code from proceeding further inside the loop.

In the second example, the foreach loop iterates over an array, and when the value of $val equals "stop," the break statement is executed, causing the loop to terminate prematurely. You can also use the optional argument with break to achieve the same result, such as break 1;.

The third example demonstrates the usage of the optional argument with a switch statement. Inside the switch, when the value of $i is 5, the break 1; statement is executed, which exits only the switch statement. When the value of $i is 10, the break 2; statement is executed, which exits both the switch and the enclosing while loop.

The break statement provides flexibility in controlling the flow of your program by allowing you to terminate loops or switches based on specific conditions or desired control flow. It helps you control the execution of your code and achieve the desired behavior.

Remember that the break statement can only be used within loop or switch structures. It cannot be used outside of these structures.

In addition to the break statement, Sputnik provides two convenient variations: BreakIf(<condition>) and BreakUnless(<condition>). These variations allow you to conditionally break out of a loop or switch based on the specified condition.

The BreakIf(<condition>) statement will break out of the immediate enclosing loop or switch if the given condition evaluates to true. On the other hand, the BreakUnless(<condition>) statement will break if the condition evaluates to false.

These variations offer a concise and expressive way to control the flow of your loops and switches based on runtime conditions. They allow you to break out of the structure only when certain conditions are met, providing you with more flexibility and control over your program's execution.

Please note that the conditions provided to BreakIf(<condition>) and BreakUnless(<condition>) should be valid expressions that can evaluate to true or false. These variations can be particularly useful when you want to add conditional breaking logic without explicitly using an if statement.

Overall, the break statement, along with its variations BreakIf(<condition>) and BreakUnless(<condition>), empowers you to tailor the behavior of your loops and switches based on specific conditions, allowing for more dynamic and controlled execution paths in your code.

Related

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

Example

Using it on a While statement:

my $value = 0;
while (true)
{
    say "Hello World";
    break; // Prevent the code going any further
    say "Testy";
}
// PRINTS
// Hello World

Using it on a Foreach statement:

my $arr = array("one", "two", "three", "four", "stop", "five");
foreach (my $val in $arr)
{
    if ($val == "stop")
        break; // You could also write 'break 1;' here.
    say $val;
}
// PRINTS
// one
// two
// three
// four

Using the optional argument:

my $i = 0;
while (++$i)
{
    switch ($i)
    {
        case 5:
            say "At 5";
            break 1; // Exit only the switch.
        case 10:
            say "At 10; quitting";
            break 2;// Exit the switch and the while.
        default:
            break;
    }
}
// PRINTS
// At 5
// At 10; quitting

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

my $i = 5;
switch ($i)
{
    case 5:
        say "Hello";
        breakIf($i == 5);
        say "World!";
}
// PRINTS
// Hello

my $i = 5;
switch ($i)
{
    case 5:
        say "Hello";
        breakUnless($i == 5);
        say "World!";
}
// PRINTS
// Hello
// World!

 


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