Does a return break a loop?

  1. 6 months ago

    Vincent V

    19 Dec 2018 Amsterdam, The Netherlands
    Edited 6 months ago

    Another question if I may.
    I'm having an issue with a For Next loop that is terminated prematurely. In the loop is a NSTask.

    For t = 0 To 4
    
    task = new MyTask
    Dim Arguments( -1 ) As String
    Arguments.Append "-input" + Cstr(t)
    Arguments.Append "x"
    Arguments.Append "-output"
    Arguments.Append "y" + Cstr(t)
    task.setArguments Arguments()
    task.setStandardOutput outputPipe
    task.launch
    return
     
    Next

    Instead of 5 times, the loop runs only 1 time. Apparently the 'return' statement (part of the NStask), causes the loop to exit after the first time.
    What could be wrong and how do I prevent it from exiting the loop?

  2. Tim P

    19 Dec 2018 Pre-Release Testers Rochester, NY

    Yes, a return exits a loop. It exits the method as well.

  3. Paul R

    19 Dec 2018 Pre-Release Testers, Xojo Pro Kirkland, WA

    Remove the “return”.

  4. Vincent V

    19 Dec 2018 Amsterdam, The Netherlands

    Removing the return freezes the whole app. I need to think of some other solution.

  5. Paul R

    19 Dec 2018 Pre-Release Testers, Xojo Pro Kirkland, WA

    Possibly you should execute the code via a timer to give the launch time to execute.

  6. Seth O

    20 Dec 2018 Pre-Release Testers, Xojo Pro Florida/England/California/NY/...

    Although you might have intended the RETURN to be part of the task, the way you have the code, it is not:

    The RETURN in your code is a standalone statement (just like the other 11 lines of code) and it's doing exactly what it is documented to do: RETURN always IMMEDIATELY ends whatever function/method it is part of--regardless of any loops that might have been in-progress--and it returns the specified value to whatever routine triggered this function.

    Since there is nothing else on the same line as the RETURN, it is not sending back a value (effectively nil), but it is still ending all loops and skipping any remaining code in this function.

    If you want the return statement to be part of the task, it needs to be in the code of the task itself--not here. If there's no RETURN in the code of the task itself, that could be why the app freezes--your task may have an infinite loop in it, preventing it from completing.

or Sign Up to reply!