Hi guys,

While I was creating a program, I came across this situation where using Goto inside an If...:Then inside a loop to go to a Lbl inside that loop, the program will exit the loop after that execution. For example,

Code:
For(A,1,3
If 1:Then
Goto A
End
Lbl A
Disp 1
End

will only display 1 a single time, before exiting the loop. Note that an If...:Then loop is required; just using If with one command after it doesn't interfere with the loop.
Does anyone know why they did this or a clever way to get around it? I suppose it's a good way to intentionally exit loops, but really, handcrafting loops everywhere in your program with other Lbl's does screw up the looks and clarity of your program...
Carthago wrote:
Hi guys,

While I was creating a program, I came across this situation where using Goto inside an If...:Then inside a loop to go to a Lbl inside that loop, the program will exit the loop after that execution. For example,

Code:
For(A,1,3
If 1:Then
Goto A
End
Lbl A
Disp 1
End

will only display 1 a single time, before exiting the loop. Note that an If...:Then loop is required; just using If with one command after it doesn't interfere with the loop.
Does anyone know why they did this or a clever way to get around it? I suppose it's a good way to intentionally exit loops, but really, handcrafting loops everywhere in your program with other Lbl's does screw up the looks and clarity of your program...

Plz dont do this, you can use variables as arguments for the for loop if the number of times it loops around has to change, but you should never use a Goto inside a loop or an If-Then conditional, this causes memory leaks, which will slow down your program and could even cause it to crash if you do it too much.
Thanks for your quick reply. I was actually using the Goto inside some giant For( loop in which I wanted to exit other loops... quite the mess, to be fair.
So what you're saying is that you should never use a Goto inside any loop or If. Does that also include the single-argument If without Then? I hope not, cos otherwise I've got some serious adapting to do to my programs...
Carthago wrote:
Thanks for your quick reply. I was actually using the Goto inside some giant For( loop in which I wanted to exit other loops... quite the mess, to be fair.
So what you're saying is that you should never use a Goto inside any loop or If. Does that also include the single-argument If without Then? I hope not, cos otherwise I've got some serious adapting to do to my programs...

No, luckily for you, it does not include the single if without the Then and End statements, this has to do with the fact that the OS will not consider a single if statement as a layer (not sure I'm using the correct terminology here, don't quote me on this Razz)
The single If within a loop seems to work smooth, does this also cause memory issues?
This may be of use to you. Smile
Carthago wrote:


Code:
For(A,1,3
If 1:Then
Goto A
End
Lbl A
Disp 1
End



The reason this breaks the For( is clear when you know the following two facts.

First, this is how a For(var,start,end loop works:

* Set var to start
* If var is greater than end already, jump to the next End.
* Push something onto the stack to remember we're in a For( loop.
* Execute code until you hit an End.
* Increment var
* If var is greater than end, pop the thing off the stack to end the loop.
* Otherwise, jump back to the first line of the For( loop.

If:Then works the same way, except without the looping. If without Then doesn't push anything to the stack, so it doesn't need End.

Second, when the parser hits a Goto/Lbl, it *forgets where it is* in the program. The next End the parser encounters goes to the control flow construct at the top of the stack.

Therefore, this is what's going on: Inside the For(, inside the If:Then, the parser hits the Goto and skips the End, thinking it's still in the If:Then. Then it displays 1, and the End ends the If:Then, but the calculator thinks it's still in the For(. Then it gets to the end of the program, so all loops automatically break.

Skipping over End with Goto can be done on purpose, but it's tricky to get right. As Michael linked you, memory leaks must be avoided, which can get slightly complicated, especially if there are multiple Goto going to the same Lbl.
lirtosiast wrote:
Skipping over End with Goto can be done on purpose, but it's tricky to get right. As Michael linked you, memory leaks must be avoided, which can get slightly complicated, especially if there are multiple Goto going to the same Lbl.


Are you talking about the Goto-based sub-routines (internal sub-programs) trick? JWinslow23 taught me something about that for First Fantasy II a few weeks ago and I think he mentioned that you told him how to do it. I forgot if it was you who taught him in the first place, but this is what I mean:


Code:
Goto 0
Lbl SP
<sub-routine code>
End

Lbl 0
For(Z,⁻1,0
If Z
Goto SP
End


I've used this for a few weeks (to avoid having multiple sub-programs other than CE Textlib) and have yet to run into any memory leak. I'm still confused about how it works, though, because there are two End's in there, and also why he told me to use -1,0 instead of 0,1.
DJ_O wrote:
lirtosiast wrote:
Skipping over End with Goto can be done on purpose, but it's tricky to get right. As Michael linked you, memory leaks must be avoided, which can get slightly complicated, especially if there are multiple Goto going to the same Lbl.


Are you talking about the Goto-based sub-routines (internal sub-programs) trick? JWinslow23 taught me something about that for First Fantasy II a few weeks ago and I think he mentioned that you told him how to do it. I forgot if it was you who taught him in the first place, but this is what I mean:


Code:
Goto 0
Lbl SP
<sub-routine code>
End

Lbl 0
For(Z,⁻1,0
If Z
Goto SP
End


I've used this for a few weeks (to avoid having multiple sub-programs other than CE Textlib) and have yet to run into any memory leak. I'm still confused about how it works, though, because there are two End's in there, and also why he told me to use -1,0 instead of 0,1.


This works because the Goto 0 causes the first End to be skipped, then the Goto SP skips the End associated with the For( loop. Because the parser still thinks that it is inside the loop, it doesn't throw an error when it reaches the first End. At that point, Z is incremented from -1 to 0, so Z is false and the Goto SP is not called.

The For( loops starts at -1 so that Z is initially true and changes to false. Starting at 0 would cause Z to be false, so the Goto SP would not be called on the first iteration. A similar result can be achieved by counting down with For(Z,1,0,-1).
Yeah, what I was confused about is why no ERR:SYNTAX happens when reaching the second End (which is usually what happens when we have one too many End instructions, right?)
Because the code only ever uses one End. Either Z!=0 and the code goes to SP, where it "Ends" the loop and starts again with incremented Z, or it doesn't go to SP, and End the loop with the actual End command of the loop. If this makes any sense.
thanks.
What I use in such cases, is such code as this:

Code:
While 0
  Lbl 0
End
For(Z,~1,0
  If Z
  Goto 0
End

or

Code:
For(Z,~1,0
  If Z
  Goto 0
End
While 0
  Lbl 0
End
So if I wanted to use Goto's in different parts inside a loop to go to a Lbl further in this loop, I should raise both the Goto's and the Lbl to the highest level any of them is at using While 0 or Repeat 1 style empty loops?
Carthago: I suggest that for now you don't use Goto at all. Since the 1970s, programmers have moving away from Goto statements except in specialized applications, because code with many Goto is generally harder to understand and modify. TI-BASIC's Goto has the additional disadvantage of being slow-- when the calculator gets to a Goto, it searches *every line* of a program until it finds a Lbl.
That's kinda bad indeed, I would have thought it to work more like the Assembly versions, or would scan the whole program in advance and save the location of the labels or something. Guess I'll have to try and figure something out with additional variables and If's then.
Thanks a lot for all your help guys, it's been very helpful.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement