Use of Goto inside an If:Then or other loop.

Since entering the TI community, i have been constantly informed that i cannot have a Goto inside a loop. However, this can be slightly limiting in terms of flexibility, so i set out to decide how it could be done... and i have succeeded.

If you have the program:
:Lbl 1
:While G=0
:GetKey->G
:If G=105:Then:Goto 2:End
:End
:Lbl 2
:[code]:Goto 1

you will notice that the program gets slower and slower, eventually giving off an ERR:MEMORY. So, how can you fix this? The answer is so simple it's been smacking me in the face for the past week. The TI-OS doesnt care where you end your If:Then statement, just so long as it gets ended at some point. So, knowing this, our program becomes:
:Lbl 1
:While G=0
:GetKey->G
:If G=105:Then:Goto 2:End
:End
:[code]:Goto 1
:Lbl 2
:End
:[code]:Goto 1

If you were to run this program, you would experience absolutely no slowdown. If you dont believe me, run any of the past 2 programs i have released, as both use this technique as a test.

you must also ensure that the only way the End can be accessed is through an If:Then, otherwise the TI-OS wont know what it is supposed to be ending, and will give out an ERR:SYNTAX

This is useful for certain repetitive programs, for example in a store program
:Lbl A
:If M <= 100:Then:Goto 2:End
:[code]
:Goto 1
:Lbl B
:If M <= 150:Then Goto 2:End
:[code]
:Goto 1
:Lbl 2
:End
:Disp "you dont have","enough money"

by using this method, you only save about 30 bytes for each item you have in the store, as you only have to enter the "you dont have enough money" message once, instead of once per item.
That's a good solution if you MUST have a goto inside of an If Then. However, I still stand by my position that you should NEVER use goto's....
Why not use the the trick about not using End's at all for those one liners. Like you can just as easily do:


Code:

If M<=100
Goto 1
(code)



Lbl 1
(code)


Notice how you don't need an end. This is because the processor checks to see if the conditional is true, if it is it checks the next line and if it is not a then, it will continue as normal; if it is, it skips until it hits an else or End. Therefore this method if perfect for IF Goto type statements. As for within while or repeat loops I prefer this setup:


Code:

DelVarADelVarB
While not(A
Repeat B
getkey->B
End
If B=105 or B=11
1->A
End

If B=105
Then
code for enter
End
If B=11
Then
code for Y=
End


That's just how I do it to avoid program fatigue Smile
true... but if you want to execute multiple conditions, then have a Goto...
Yeah, cool but a hair complex. Smile
complex? not in the least... it seems that way til you get used to it... just remember to place the End after the Lbl and you're good...

(Note: can this be in teh next B1337 newsletter, or is it not 1337 enough? considering i just proved everyone at calcgames.org wrong...)
alex10819 wrote:
(Note: can this be in teh next B1337 newsletter, or is it not 1337 enough? considering i just proved everyone at calcgames.org wrong...)


err... no, not EVERYONE at CG... after I realized the problems with Goto's inside "IF:Then", "For(", "Repeat", or "While" I realized this...


Code:

:If 1:Then
<code>
:Goto 1
:End
<code>
:If 1:Then
:Lbl 1
:End
<code>


thusly it is good, the TI-OS searches for an "End" after one of the clasues I listed, and if no end is encountered it holds RAM until it finds it... that's why in SOME conditions A Dot RPG goes ERR:MEMORY quickly and sometimes not Very Happy

and I agree with Alex... I am elete Razz
alex10819 wrote:

:Lbl 1
:While G=0
:GetKey->G
:If G=105:Then:Goto 2:End
:End
:[code]:Goto 1
:Lbl 2
:End
:[code]:Goto 1


nice tip, but it seems klunky, as this would work just as well:

[code]:While G=\=105
:GetkKey->G
:[stuff here]
:End
:[lbl 2 stuff here]
[/code]

i believe that would also be more effecient, as it removes the unecessary g=0 check, and the slow goto's (obviously, this is only for that small example. The goto in a loop is nice, but i don't like it. It seems goto's are real slow - so i always try and find ways around it) This could come in handy in prgrm's that aren't speed dependant, like most non-games

Or here's a faster version of yours (i think):

alex10819 wrote:

:Lbl 1
:Repeat GetKey
:If ANS=105:Then:Goto 2:End
:End
:[code]:Goto 1
:Lbl 2
:End
:[code]:Goto 1
  
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