Business Central 2025 wave 1 (BC26): Continue statement (Skip the current iteration of a loop)

Dynamics 365 Business Central

Hi, Readers.
The public preview for Dynamics 365 Business Central 2025 release wave 1 (BC26) is available. Learn more: Link.

I will continue to test and share some new features that I hope will be helpful.

This new feature is not yet documented in the Business Central 2025 release wave 1 (BC26) release plan but is mentioned in AL Language extension changelog Version 15.0

Continue statement
From runtime version 15, it is possible to use the continue keyword in loops to continue to the next iteration.

https://marketplace.visualstudio.com/items/ms-dynamics-smb.al/changelog

In AL language, we often use repetitive statements.
A repetitive statement is also known as a loop. The following table shows the looping mechanisms in AL. More details: AL repetitive statements

Looping mechanismDescription
forRepeats the inner statement until a counter variable equals the maximum or minimum value specified.
foreachRepeats the inner statement for each statement in a List, XmlNodeList, XmlAttributeCollection, or JsonArray.
whileRepeats the inner statement as long as the specified condition is true. The statement in a loop of this kind is repeated zero or more times.
repeatRepeats the inner statements until the specified conditions evaluate to true. The statements in a loop of this kind are always executed at least one time.

Until now, we have only been able to use break statement but no continue statement in AL language.
Break statement: You use the break statement to terminate the iterative statement in which it appears.

You typically use the break statement in the repeating statements such as forwhile, or repeat to stop an iteration or loop when certain conditions are met. For example,

So if we only want to skip 6, we can’t do it using break statement. We need to add some complex conditions.

With this wave (BC26), we can do this easily via a Continue statement👏👏👏. The continue statement in AL Language is the same as in other programming languages, it is a jump statement used to skip the current iteration of a loop and continue with the next iteration. It is used inside loops (for, while, or repeat) along with the conditional statements to bypass the remaining statements in the current iteration and move on to next iteration. Let’s take a look at an example:

Great.

Test code:

pageextension 50102 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        i: Integer;
        Msg: Text[250];
    begin
        while i < 10 do begin
            i := i + 1;
            if i <> 6 then
                Msg := Msg + Format(i) + '\'
            else
                continue;
        end;
        Message(Msg);
    end;
}

The same is true when using the for-to and for-downto statements.

Test code:

pageextension 50102 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        i: Integer;
        Msg: Text[250];
    begin
        for i := 1 to 10 do begin
            if i = 6 then
                continue
            else
                Msg := Msg + Format(i) + '\'
        end;
        Message(Msg);
    end;
}

Let’s look at another simple process. Displays the customer names except the customer number 20000:

Test code:

pageextension 50102 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        Cust: Record Customer;
        Msg: Text[250];
    begin
        Cust.Reset();
        if Cust.FindSet() then begin
            repeat
                if Cust."No." = '20000' then
                    continue;
                Msg := Msg + Cust.Name + '\';
            until Cust.Next() = 0;
        end;
        Message(Msg);
    end;
}

Great. Give it a try!!!😁

PS: Business Central 2023 wave 1 (BC22): Iterating with foreach on Text variables

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL