5 Replies Latest reply on Feb 1, 2016 4:06 AM by jasheldo

    Nested Loops:  Stopping only the innermost loop when a condition is met

    jasheldo

      I'm working on a project and the "Exit Loop If[]" command isn't operating as I would expect it to.  I'm confident this is just me not knowing what's happening but here's an outline.

       

      Go to Layout [ Original Layout ]

      Go to Record/Request/Page [ First ]

      Loop

           Go to Related Record [ Another Table ; Using Layout BLAH ]

           Loop

                if [ $x = 1 ]

                     Set Variable [ $variable ; 1 ]

                     Exit Loop If [ 1 ]

                else

                     DO SOME STUFF INVOLVING $x

                end if

           Go To Record/Request/Page[ Next ; Exit after Last ]

           End Loop

      Go to Layout [ Original Table ]

      Go To Record/Request/Page [ Next ; Exit After Last ]

      Set Variable [ $x ; 0 ]

      End Loop

       

      More or less this is what I'm doing.  What I'm noticing is the "Exit Loop If" above is exiting both loops.  I need to only make the inner loop end and continue operating the outer loop.  In order to verify this behavior I ran through the script debugger and sure enough when the "Exit Loop If" line was executed it left both loops.  If this is truly a trivial thing and I'm not privy to a very obvious function (e.g. Halt Script vs Exit Script ) please just let me know what I need to use instead of "Exit Loop If".

       

      Thank you.

        • 1. Re: Nested Loops:  Stopping only the innermost loop when a condition is met
          wimdecorte

          The "exit loop if" is never going to exit both loops, so it is the right one to use.  So I think the problem is elsewhere but hard to say with just this script.

           

          If you step through it with the script debugger; do you see any other scripts triggered by events as you move between records and layouts?

          • 2. Re: Nested Loops:  Stopping only the innermost loop when a condition is met
            BruceRobertson

            This is one very mixed up script.

            There is only one place you actually declare $x; and there you set it to 0.

            At the point where you declare "do something with $x" $x does not exist.

            Also, you set another variable - $variable but you never use it.

            • 3. Re: Nested Loops:  Stopping only the innermost loop when a condition is met
              jasheldo

              Thank you for the quick reply winmdecorte.  I have the variable list open and have checked everything (granted, I completely understand that when you're proof reading your own work you'll still miss obvious things).  I'll try walking through it again but honestly I've no expectation at all that the outer loop should be exiting.  It should move on to the next record in the parent table and then go back to the top of the loop, enter the child table and carry on.  Instead, both loops are terminating.

              • 4. Re: Nested Loops:  Stopping only the innermost loop when a condition is met
                justinc

                Hey Jasheldo,

                    I think that FileMaker is working the way that you wold expect it to...but that the design of your looping logic is broken somehow.

                 

                I created a test script with two simple loops.

                 

                Screen Shot 2016-01-30 at 10.19.19 .png

                 

                At the end the dialog box shows that I = 5 and J = 5.  If the inner loop were exiting both loops, then the J value would only = 1.

                 

                And the better test, stepping through the loop, when the inner loop hits 'Exit...' it jumps to line 10, runs lines 10 and 11, hits 12 - which causes it to go back to line 5.  So it is only exiting the inner loop.  (I even tried it where line 8 was:  "Exit Loop if [1]" so it always exited, and it still correctly looped the outer loop.)

                 

                Is the script you posted actually how it is set up?

                • 5. Re: Nested Loops:  Stopping only the innermost loop when a condition is met
                  jasheldo

                  Thank you everyone for your feedback!  What I typed above in the original post wasn't the original loop verbatim but a brief illustration of what I've been trying to do.  Bottom line, you all unanimously confirmed for me what I was suspecting but haven't been able to find in writing:  "Exit Loop If[]" exits ONLY one single loop and it exits only the loop you're immediately in.  More aptly put, I did something boneheaded to make my loop exit everything and need to do further digging to see what it was.

                   

                  I appreciate all the feedback and the time from everyone.