Exit from an IF/ENDIF loop with a GOTO


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2008
    Location
    Italy
    Posts
    825

    Default Exit from an IF/ENDIF loop with a GOTO

    In a time sensitive application I did try to gain time jumping out of a IF/ENDIF loop with a goto instruction. Here a snippet as example.
    Code:
    Main_Loop
    
    If A > B then
    A = B
    Gosub one
    Goto skip_00
    ENDIF 
    
    If C > D then
    C = D 
    Gosub two
    Goto skip_00
    ENDIF 
    .
    .
    .
    .
    .
    .
    .
    If  Z > W then 
    Z = W
    Gosub ten
    ENDIF
    
    Skip_00:
    
    Goto Main_Loop
    The code seems working without any problems and does what is suppose to do.
    The question is: Exiting any loops with a goto instruction can produce unpredictable results?

    Thank you for any clarification on the subject.

    Al.
    All progress began with an idea

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

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

    Hi Al,
    The only scenarios where problems can (and will) pop up that I can think of right now are:

    A) Jumping out of an ISR with a GOTO without eventually reaching a RETFIE or INT_RETURN or whatever is appropriate for the type of interrupt you're using.

    B) Jumping out of subroutine to which you've GOSUBed without eventually reaching a RETURN.

    Using GOTO within an IF/ENDIF block or a WHILE/WEND, DO/LOOP type of loop structure should be a problem - as far as I can see.

    /Henrik.

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

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

    Hi Henrik, thank you for the answer.

    Let me add few more information, the code with the GOTO within several IF/ENDIF loops is now running past the 48 hours. Thre program has the watchdog off and is monitored via serial connection by a pc which sends every 60 secs a series of bytes with random numbers and check the answer. So far no errors has been detected and no hangs of the pic has been experienced (this was my first worry).

    So far it seems that you can exit the IF/ENDIF loop with a goto without any consequence, but I don't consider at all, this test of mine as a complete test. I will let the pic running for a whole week and see what heppens.

    Cheers

    Al
    All progress began with an idea

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

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

    Hi Al,
    I wouldn't call an IF/ENDIF construct a loop but anyway, I certainly wouldn't worry about it. I mean, what reason could there possibly be preventing it from working? I do admire your desire to test and verify though, nice job!

    Again, if that IF/ENDIF construct was within a subroutine to which you jumped with a GOSUB, THEN a GOTO someplace could (will)mess it up if it doesn't eventually end up at a "matching" RETURN.

    /Henrik.

  5. #5
    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.

  6. #6
    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.

  7. #7
    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.

  8. #8
    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