Breaking Loops

bbrownbbrown Member Posts: 3,268
edited 2006-06-28 in NAV Tips & Tricks
You can break a loop by placing the loop in its own function and using an EXIT statement within the loop.
There are no bugs - only undocumented features.

Comments

  • DenSterDenSter Member Posts: 8,307
    That's great to hear, but it is kind of abstract. Could you post a code sample? :mrgreen:
  • bbrownbbrown Member Posts: 3,268
    Example:

    Record1 = Global Rec Variable

    IF BreakTest THEN BEGIN...(Use Record1)

    Function BreakTest : Boolean

    IF Record1.FINDSET(TRUE, FALSE) THEN
    REPEAT
    IF (Condition True) THEN
    EXIT(TRUE);
    UNTIL Record1.NEXT = 0;
    EXIT(FALSE);
    There are no bugs - only undocumented features.
  • DenSterDenSter Member Posts: 8,307
    That's a good idea and a different approach. For straightforward loop breaking though I prefer an additional boolean variable inside the loop:
    IF MyRec.FINDSET THEN BEGIN
      LeaveTheLoop := FALSE; 
      // don't really need this, since FALSE is default boolean value
      // but I like to program explicitly
      REPEAT
        // do the code here
        IF Condition Met THEN
          LeaveTheLoop := TRUE;
      UNTIL (MyRec.NEXT = 0) OR LeaveTheLoop;
    END;
    
  • ara3nara3n Member Posts: 9,256
    DenSter
    I think bbrown solution solves another problem that your code will not. and that is the current record that MyRec will point once the function has existed.
    In your code MyRec will point to the next record, while bbrown code the record will point the current record he is comparing to.

    makes sense?
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • bbrownbbrown Member Posts: 3,268
    Denster,

    I think both of the approaches have their applications. I would probably refer to your approach (not to get picky) as Terminating a Loop as it will process a loop iteration in its entirety and determine at the end whether or not to continue the loop. This may not be significant in a small loop but could have an impact in a large loop.

    By small vs. large I am referring to the number of commands in the loop and not the size of the record set.
    There are no bugs - only undocumented features.
  • DenSterDenSter Member Posts: 8,307
    ara3n wrote:
    DenSter
    I think bbrown solution solves another problem that your code will not. and that is the current record that MyRec will point once the function has existed.
    In your code MyRec will point to the next record, while bbrown code the record will point the current record he is comparing to.

    makes sense?
    Makes sense, but in my code you do what you need to do with the current record inside the loop. If you need to save the current record for after the loop you have other ways to do that. I would not do MyRec.NEXT until *after* I am done with the current record.
  • DenSterDenSter Member Posts: 8,307
    bbrown wrote:
    Denster,

    I think both of the approaches have their applications. I would probably refer to your approach (not to get picky) as Terminating a Loop as it will process a loop iteration in its entirety and determine at the end whether or not to continue the loop. This may not be significant in a small loop but could have an impact in a large loop.

    By small vs. large I am referring to the number of commands in the loop and not the size of the record set.
    It really depends on what you want to do. I've always been able to program an escape, either by adding the exit condition at the end, or write a function to process one record in which you can always add an EXIT statement. I prefer not to give loop control over to another function, unless there's a sub-loop if you know what I mean. You're right though, there's an application for both approaches, and it's a matter of personal style as to what you like, I don't think there's a *right* or *wrong* way to do it, as long as it works :)
  • bbrownbbrown Member Posts: 3,268
    C/Side could really use a GoTo command.
    There are no bugs - only undocumented features.
  • DenSterDenSter Member Posts: 8,307
    bbrown wrote:
    C/Side could really use a GoTo command.
    Nah that's what functions are for :mrgreen:
  • ara3nara3n Member Posts: 9,256
    GoTo are horrible and make the system complicated to follow. That's what I remember from my cs classes.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • kinekine Member Posts: 12,562
    edited 2006-06-26
    Please, no GOTO and GOSUB... it is not command for structured language... it was used in Basic where you had no procedures and functions (I am not talking about Visual Basic but about old simple Basic used on 8bit computers as Atari etc.)... And of course in Assembler (JNZ,JZE,JMP etc.)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,112
    I concur: no GOTO's. They create, what is called spaghetti-programs. You know where your program starts, but not where it goes and even less where it ends.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    kriki wrote:
    I concur: no GOTO's. They create, what is called spaghetti-programs. You know where your program starts, but not where it goes and even less where it ends.
    And you, Italians, are very good at it ... the spaghetti I mean :lol:
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • krikikriki Member, Moderator Posts: 9,112
    kriki wrote:
    I concur: no GOTO's. They create, what is called spaghetti-programs. You know where your program starts, but not where it goes and even less where it ends.
    And you, Italians, are very good at it ... the spaghetti I mean :lol:
    BTW:I am still Belgian :!: This because a belgian law says that I lose the belgian nationality if I ask for another nationality. Only if the initiative comes from the other country, a belgian keeps also the belgian nationality.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • DenSterDenSter Member Posts: 8,307
    And you, Italians, are very good at it ... the spaghetti I mean :lol:
    :lol: very funny Luc
  • krikikriki Member, Moderator Posts: 9,112
    DenSter wrote:
    And you, Italians, are very good at it ... the spaghetti I mean :lol:
    :lol: very funny Luc
    No, very good! :lol:
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.