Dear Friends,
In a project, I have used two successive "goto" codes. As the project shows erratic operation i.e. sometimes the MCU gets locked, I fear if the two successive "goto" codes make any problem. Please confirm whether it is beyond the rule.
perhaps you could try a "gosub" command...in basic, goto will cause a "jump" to another section of code and will completely exit the current block of code where as the gosub will allow a return to the line of code immediately following the "gosub" command when the subcode is finished executing using the simple "return" command. hope this helps
Dear Friends,
In a project, I have used two successive "goto" codes. As the project shows erratic operation i.e. sometimes the MCU gets locked, I fear if the two successive "goto" codes make any problem. Please confirm whether it is beyond the rule.
Two goto's like that is perfectly fine, and the normal way of doing it - although, depending on exactly what's going on, it saves a 'goto' by inserting the second 'coto code' actually at the second goto address..
Thanks Mr. Pommie. Yes, there was a problem in my code and after rectification of the same the project is working fine with its two successive "goto"s. Thank you again.
Those GOTOs won't be executed one after the other, and there should be no problem with the code.
I once had a problem with code running three context jumps (GOTOs and Returns) in quick succession. That was caused by insufficient suppression capacitors.
From what I remember, a "goto" does not set a return address in a PIC chip and thus you must put a goto at the end of the sub-rutoutine that you are "going-to".
You must also put a label at the front of the second "goto" so that the micro goes back to the correct location, after executing the first sub-routine.
I really don't know what you mean by this statement: " A 'GOTO' doesn't set a return address in any processor, because it's not a sub-routine. If you want a sub-routine use 'GOSUB' instead."
You can use "goto" to go to a sub-routine but the last instruction in the sub-routine will always take you back to the location contained in the last instruction.
The original request was to validate two goto instructions in a routine and I considered the possible problems with these two instructions.
The possibilities were not addressed by any of the respondents.
You cannot use goto to call a subroutine only to make spaghetti code. The whole point of a subroutine is so it can be called from various locations, putting a goto at the end of it means it is not a subroutine but part of the main code.
You can use "goto" to go to a sub-routine but the last instruction in the sub-routine will always take you back to the location contained in the last instruction.
That's not a sub-routine, it's just a normal routine - a sub-routine can be called from anywhere, and automatically returns to where it came from.
The original request was to validate two goto instructions in a routine and I considered the possible problems with these two instructions.
The possibilities were not addressed by any of the respondents.
Sorry, it had been fully covered - your mistake is assuming goto can call sub-routines, which it can't (except for certain specific cases, where a subroutine has already been called, and you're changing the exit point - in which case it doesn't return to the goto, but to the original call).