Announcement

Collapse
No announcement yet.

Exit from sub inside a loop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exit from sub inside a loop

    I need to know if I will cause chaos if I continually exit from a subroutine while in a FOR - NEXT loop or an IF - END IF.

    For example:

    Sub TEST
    FOR b = 1 to 100
    IF b = 50 THEN
    a$ = "Help"
    EXIT SUB
    END IF
    NEXT b
    END SUB

    Will this cause a problem if I continually EXIT from the SUB in the middle of the FOR - NEXT loop or the IF - END IF.

    I hope someone can actually understand what I mean.

    Regards,
    Brian.

    Brian.

  • #2
    It will work fine - you are doing everything perfectly "legally".

    PowerBASIC cleans up the stack frame for the sub/function when execution of the sub/function terminates - this is handled automatically for you, and is one of the benefits of using a high-level language like PowerBASIC. In fact, this type of operation is exactly how the EXIT statement is designed let you to work. All memory used by local variables and arrays are automatically released.

    The only thing you would have to clean up yourself would be GDI objects (brushes, pens, bitmaps, or Device Contexts, etc) or memory allocations that were created with API functions (GlobalAlloc(), etc), rather than native PowerBASIC code.


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Good question!

      But i also wish to point out a great PB feature:

      Exit, Exit

      This is possible to leave 2 (different) loops..
      Check the help..

      This helped me very well and check out iterate as well.



      ------------------

      Comment


      • #4
        Thanks for your help. That will solve a few problems for me.
        Regards,
        Brian.


        ------------------
        Brian.

        Comment


        • #5

          But i also wish to point out a great PB feature:

          Exit, Exit
          You know, I never knew until now that PB had implemented that in the new compiler (also the variations with 'ITERATE').

          Darn, I wish I had printed manuals to browse!

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Darn, I wish I had printed manuals to browse!
            Available for $29.95 from PowerBASIC sales department. No offense to the online help, but the printed manual makes it much easier to understand!

            Frank

            ------------------

            Comment


            • #7
              I decompiled it using HelpDeco.

              Printed it ... and had fun to read..
              The problem is, i can't remember all of it so i need the help



              ------------------

              Comment


              • #8
                When I EXIT from SUB does it also close any files that may be open?

                Regards,
                Brian.


                ------------------
                Brian.

                Comment


                • #9
                  Brian --

                  No, not unless you are exiting from your PBMain or WinMain function and thereby closing the entire program. Exiting from SUBs or FUNCTIONs that are "below" the "main" function has no effect on open files.

                  -- Eric

                  ------------------
                  Perfect Sync: Perfect Sync Development Tools
                  Email: mailto:[email protected][email protected]</A>



                  [This message has been edited by Eric Pearson (edited June 05, 2000).]
                  "Not my circus, not my monkeys."

                  Comment

                  Working...
                  X