Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

"continue" statement in repeat until loops??

(2) ShareShare
ReportReport
Posted on by 20
I must be doing something wrong because I cannot get the "continue" statement to work.

Maybe my understanding is wrong but in the following code I would expect the "continue" statement to continue with the next record in the inner repeat..until loop but it continues on the outer loop ????

BC version: 
26.0.30643.33009
codeunit 72220 "Test"
{
procedure test()
var
items: record "Item";
i: integer;
begin
for i := 1 to 15 do
begin
items.FindSet();
repeat
if (items."No." <> '01054') then continue; // <-- This continues with "items.FindSet()" ????
Message(i.ToText());
until items.Next() = 0;
end;
 
end;
}
??

Is this the correct behaviour ? If so I expect a lot of weird stuff happening in BC going forward as this "continue" statement gets used :-)

According to the documentation "repeat" loops are supported ??
https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-al-control-statements#continue-statement 
  • Suggested answer
    Suresh Kulla Profile Picture
    46,918 on at
    "continue" statement in repeat until loops??
    I don't think there is a good documentation how continue works in nested loops for AL, it should be applying that to the inner loop rather than outer loop.
  • MO-19112130-0 Profile Picture
    20 on at
    "continue" statement in repeat until loops??
    Thanks for answering.
    Afternoon here in Denmark btw :-)

    Coming from c#, java and Golang, where "continue" works as expected I dont quite get why AL would just skip the rest of iterations in the repeat..until loop.

    The reason for my suspicion was actually that a lot of my AL code in the real code base was *not* getting executed. This logic inserts data in other tables, but it did never happen, because of "continue" totally breaking out of the inner repeat..until construct.
     
    So in the slightly modified code If I insert an http call at // HERE, will that statement never get called ?? If not then we will start seeing pretty weird behaviours in BC :-)
     
    codeunit 72220 "Test"
    {
    procedure test()
    var
    items: record "Item";
    i: integer;
    begin
    for i := 1 to 15 do
    begin
    items.FindSet();
    repeat
    if (items."No." <> '01054') then continue; // <-- This continues with "items.FindSet()" ????
    // HERE

     
    until items.Next() = 0;
    end;
     
    end;
  • Suggested answer
    Holly Huffman Profile Picture
    5,534 on at
    "continue" statement in repeat until loops??
    Good morning, afternoon, or evening depending on your location!
     
    Clarification on Behavior:
    The "continue" statement in AL loops is designed to skip the current iteration and move to the next one.
    However, in your case, it appears to be affecting the outer loop instead of the inner repeat..until loop.
    This behavior is expected because "continue" applies to the nearest enclosing loop structure.
     
    In your code:
    repeat
        if (items."No." <> '01054') then continue; // Skips the rest of the repeat loop
        Message(i.ToText());
    until items.Next() = 0;

    Since the "continue" statement is inside the repeat..until loop but does not have additional logic after it, it effectively skips the rest of the loop iteration and moves to the next cycle of the outer for loop.
     
    Possible Solutions:
    To ensure that the "continue" statement only affects the inner loop, consider restructuring the logic:
    Option 1: Use an Additional Condition
    Modify the loop to ensure that the "continue" statement only skips the current item but does not exit the outer loop prematurely:
    repeat
        if (items."No." <> '01054') then
            if items.Next() = 0 then break; // Ensures the repeat loop continues correctly
        Message(i.ToText());
    until items.Next() = 0;

    Option 2: Use a While Loop Instead
    If restructuring the repeat..until loop does not work as expected, consider using a while loop for better control over iteration flow.
    Additional References:
    Microsoft has introduced "continue" support for AL loops in Business Central 2025 Wave 1 (BC26). However, there are still some nuances in how it interacts with nested loops. You can find more details in the official documentation here.
     
    Hope this helps some!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,118 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans