Exit from an IF/ENDIF loop with a GOTO


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Exit from an IF/ENDIF loop with a GOTO

    To repeat what Henrik said using slightly different words, there is nothing wrong with using a GOTO to exit a loop as you are using it because that is pretty much what it was intended for. Where you can get into trouble is when you use GOTO to exit a subroutine and thereby miss a RETURN. Your code snippet does not show what's inside the subroutines "one" and "two" but for example, perhaps "one" contains the code:

    one:
    IF something = value THEN GOTO Main_Loop
    something = different
    RETURN

    In this case, whenever the program goes to "one" it puts whatever it was doing on the stack. When it hits the RETURN, it takes the values off the stack and puts them back where they were before the GOSUB, and life is good. When the condition of "something = value" is met, the clean up doesn't happen, and the old register values are left on the stack. Each time it happens, a little more memory is not released. Eventually you run out of stack space and crash.

    In your example, the GOTO is not inside a subroutine, so you don't have to worry about it. It should run forever.

    The same is true of ISRs which are just a special kind of subroutine.

  2. #2
    Join Date
    Jan 2007
    Posts
    78


    Did you find this post helpful? Yes | No

    Default Re: Exit from an IF/ENDIF loop with a GOTO

    Is it possible for you to use a WHILE loop instead of the IF/ENDIF?
    It can perform much the same function except you can modify the controlling variables upon which the WHILE test is performed, within the WHILE loop, or within a GOSUB/RETURN, and then it will exit gracefully.

  3. #3
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Exit from an IF/ENDIF loop with a GOTO

    I always thought that the ENDIF statement was more for the PBP compiler and that you could Jump out of an IF/ENDIF pair without problem.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  4. #4
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Exit from an IF/ENDIF loop with a GOTO

    Guys this has nothing to do with IF / ENDIF, WHILE, LOOP, or EIEIO (I made that one up). This has to do with GOTO.
    Please read the content of the posts instead of adding to the confusion.

Similar Threads

  1. Capture loop exit
    By spcw1234 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2012, 20:29
  2. Replies: 2
    Last Post: - 28th July 2011, 16:23
  3. Am I stupid? Goto does not GOTO??? Where's the bug??
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 15th October 2008, 10:31
  4. Timer Interrupt to exit a closed loop
    By duncan303 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 16th December 2007, 16:45
  5. Problems with IF ENDIF
    By jetpr in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd November 2005, 16:10

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts