Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: Delnar_Ersike on February 22, 2007, 08:20:00 am

Title: Is it possible to Goto a label in a loop?
Post by: Delnar_Ersike on February 22, 2007, 08:20:00 am
This is probably a question you can all answer: Is it possible to goto a lbl in the middle of a loop and still have the loop run without any RAM leaks?
Title: Is it possible to Goto a label in a loop?
Post by: rivereye on February 22, 2007, 08:57:00 am
doubt it, but haven't tired it either.
Title: Is it possible to Goto a label in a loop?
Post by: tifreak on February 22, 2007, 09:46:00 am
I am not sure, nor would I want to take the time to find out, because I try to not use Lbl and Goto. It would slow down the loop considerably everytime it ran across that Goto...
Title: Is it possible to Goto a label in a loop?
Post by: dinhotheone on February 22, 2007, 10:30:00 am
yes, it is. it was discussed in some subroutine question.

what you have to do though is return to the loop everytime. so as not to create a memory leak

ill give you a concept example and a use example
goto 1
label 0
end
label 1
while1
disp "hi"
goto 0
end


this displays hi forever. The reason being the same behind a memory leak. everytime the calc hits a while,repeat,for,if-then (i think thats al of em) the calc keeps track of it in the memory. so when you hit the end, no matter if its before of after the loop, it transports you back to the start of the loop (obviously not for if-then's of if the condition is met...) the way that it is used is in subroutines: the fastest way to find which subroutine you want is using a goto to a label at the top of the program since the compiler starts searching for the label at the top. then for each label, you have an end to transport you back to the start of the main game loop.

ie:

goto 1 "jumps over the subroutines"
label A
do the things that you gota do
end "another end if you used an if-then instead of plain if"
label1
while 1 "main game loop"
if (condition) "if you use if-then for speed then you have to have 2"
goto A ---------   -------------- "end's at the end of the subroutine"
end

this is the best way to do subroutines and does not create a memory leak as long as you are absolutely sure you hit an end for everything.
Title: Is it possible to Goto a label in a loop?
Post by: trevmeister66 on February 22, 2007, 10:52:00 am
or you could just not use goto's in a loop and not have to worry about it ;)wink.gif
Title: Is it possible to Goto a label in a loop?
Post by: dinhotheone on February 22, 2007, 11:58:00 am
its not really that bad and its the most efficient subroutine method, i just wrote alot because i like to feel smart.
perhaps if you explained why you needed to do this then i could be of more help.
Title: Is it possible to Goto a label in a loop?
Post by: Demon on February 22, 2007, 12:24:00 pm
You can also use a Lbl-Goto loop (like I do in the PyroEdit and PyroMap Bases)
PROGRAM:LOOP
:Lbl 0
:  Goto This
:  Goto That
:Goto 0
:Lbl This
:
:Goto 0
:Lbl That
:
:Goto 0
Title: Is it possible to Goto a label in a loop?
Post by: trevmeister66 on February 22, 2007, 12:53:00 pm
wouldn't that actually never get to "goto that"?
Title: Is it possible to Goto a label in a loop?
Post by: Demon on February 22, 2007, 01:16:00 pm
If Ans=Something
Goto This
Ia Ans=Something Else
Goto That
Title: Is it possible to Goto a label in a loop?
Post by: dinhotheone on February 22, 2007, 01:30:00 pm
the only problem there is that if you have alot of subroutines, then your goto-label loops have to repeatedly search through the program passing over the subroutines everytime, in this case a looping structure is far more efficient because it does not have to reread over all the useless code (useless to the os at that time) to get to the label because the start of the looping structure is temporarily stored in the calcs ram. looping structures will pretty much always be faster than label-goto loops but label-goto's are easier to program.
Title: Is it possible to Goto a label in a loop?
Post by: Delnar_Ersike on February 22, 2007, 01:44:00 pm
Thanks everyone! I think I have the answer to my question.
BTW, I was thinking of it the other way around:

:Repeat Ans>something
:(code)
:Lbl 1
:(code)
:End
:If Ans:Then
:Goto 1
:End
Title: Is it possible to Goto a label in a loop?
Post by: Radical Pi on February 22, 2007, 02:06:00 pm
As long as you make sure you have enough End's reached, you can do it with no side effects other than speed.
Title: Is it possible to Goto a label in a loop?
Post by: dinhotheone on February 22, 2007, 02:24:00 pm
the problem with the code you supplied is that when you jump into the loop, the calculator doesn't know it just jumped into the loop. it will continue execution as though it were not in a loop, up untill hte point where it hits the end where it will either error due an end with no structure (if you used a plain if) or (if you used an if-then) it will see the end as the end to the if-then and continue as though nothing happened. too jump into a loop you have to be already in the loop
ie
while condition "again, main game loop"
goto 0
label 1
end
label 0
goto 1

you can only have a plain if for the jump out/in otherwise it will mem leak.
and you must always jump back in.

the only otherway to jump 'into' a loop is to jump to the top of it
ie

label1
while condition
end
goto 1

you can't really tell the complier to remember that there is a start of the loop up above. at least not with basic, you may in asm.