As the title says, I have a If/Then/Menu/Else/Goto/End statement. I am wondering if there is a fix to this.
We'd have to see more code to be able to help you with that, to see what is going on, but using a Goto anytime a command needs an End statement to finish things off is a very bad thing.
The code for the part in question is:


Code:
If X=2
Then
Menu("   Player Two   ",Str1,R2,Str2,P2,Str3,C2,Str4,L2,Str5,S2)
Else
Goto 2
End


The strings are the options (i use them more than once)

Edit: Is it possible to do:


Code:
If X=2
Menu("   Player Two   ",Str1,R2,Str2,P2,Str3,C2,Str4,L2,Str5,S2)
If X=2
Goto 2
Basically, get out of the habit of using Gotos inside of If/Then/End statements. I think there's a trick, might be a bit advanced, of using While loops instead.

The reason being, the OS keeps track of how many If statements it enters and lowers the count when it encounters an End. If the OS cannot close an If statement, the count will get higher with every goto. Eventually this count will slow down the calculator. Once you close the program the count is zero'd. Most huge slowdowns happen after a matter of hours, unless the program is just really badly coded or the infringing Goto(s) are called repeatedly.

Edit: Your example is do able. You do not need a Then statement for only one line of instruction.
I know, I was told that in an earlier thread, but do you think that me edit above would be a good alternative?
MrDew25 wrote:
I know, I was told that in an earlier thread, but do you think that me edit above would be a good alternative?


Sorry Very Happy I do, keep in mind (and if I'm remembering correctly) that "else 2" is checking if Ans=2. But I could likely be wrong.
I am sorry for reviving an old thread, but I thought that this would be the first place to place it. I am creating a new program where I have a If/Then/Goto/End statement, but I have a Stop command before the End command, is this ok, are will it still cause a memory leak?
This will still cause the same issues.
So there is no way to stop this leak?
If not, can you tell me how to get around it?
Your best bet is to restructure your code to make it not require the GoTo. I can help with specifics if you show your code, but generally some sort of While or Repeat loop.
Here is my code right now:

Code:
If A=0
Menu("   First Time?    ","Yes",Z1,"No",Z2
Lbl Z1
"0"->Str0
"1"->Str1
"2"->Str2
"3"->Str3
"4"->Str4
"5"->Str5
"6"->Str6
"7"->Str7
"8"->Str8
"9"->Str9
"0"->Str0
Goto Z2
Lbl Z2
1->A
Goto Z
Lbl Z
ClrHome
Menu("Want To Give Up?","Yes",Y,"No",N)
Lbl Y
Disp "That's What I","Had Thought."
Pause
ClrHome
Output(1,1,"")
Stop
Lbl N
ClrHome
Disp "Fine Then,","I Warned You."
Pause
ClrHome
Goto X
Lbl X
Menu("    ? ? ? = 6   ",Str0,A,Str1,B,Str2,C,Str3,D,Str4,E,Str5,F,"Next",V)
Lbl V
Menu("    ? ? ? = 6   ",Str6,G,Str7,H,Str8,I,Str9,J,"Back",X)
Lbl A
ClrHome
If Str0="Completed"
Then
Disp "Already Complete","Try a Different","Number Now."
Pause
Goto Z
Stop
End
Disp "0 0 0 = 6","Solve for six!"
Input "",Str0
If expr(Str0)!=6 or "6"=Str0
Then
Disp "Sorry, Try Again"
Pause
"0"->Str0
Goto A
Stop
End
If expr(Str0)=6
Then
ClrHome
Disp "Congratulations!","You Beat Level","1.  Continue","With The Other","Eight Now."

Is there a suggested way that I could fix this?


Edit: Could I do this:

Code:
If ____
Goto __
Lbl __
(code)
Stop
On thing I see right away is in quite a few places you have Goto _:Lbl _, the Goto _ is not needed and the code will work exactly the same without it.

I also recommend you replace all uses of "Stop" with Return as in most cases they will act the same but when working with shells such as Doors CS Return bahaves better and leaves the calculator in a cleaner state.

Though most modern shells like Doors CS will handle Stop fine it is still better practice to use Return.

One last thing, this doesn't appear to be all of your code as how is A being set right at the start and what is even the check for A=0 doing?
To address your specific concern, I would do something like this:

Code:
If A=0
Menu("   First Time?    ","Yes",Z1,"No",Z2
Lbl Z1
"0"->Str0
"1"->Str1
"2"->Str2
"3"->Str3
"4"->Str4
"5"->Str5
"6"->Str6
"7"->Str7
"8"->Str8
"9"->Str9
"0"->Str0
Goto Z2
Lbl Z2
1->A
1->Z
While Z
ClrHome
Menu("Want To Give Up?","Yes",Y,"No",N)
Lbl Y
Disp "That's What I","Had Thought."
Pause 
ClrHome
Output(1,1,"")
Stop
Lbl N
ClrHome
Disp "Fine Then,","I Warned You."
Pause 
ClrHome
Goto X
Lbl X
Menu("    ? ? ? = 6   ",Str0,A,Str1,B,Str2,C,Str3,D,Str4,E,Str5,F,"Next",V)
Lbl V
Menu("    ? ? ? = 6   ",Str6,G,Str7,H,Str8,I,Str9,J,"Back",X)
Lbl A
1->B
While B and Z
ClrHome
If Str0="Completed"
Then
Pause  "Already Complete","Try a Different","Number Now."
0->Z
Stop
End
If Z:Then
Disp "0 0 0 = 6","Solve for six!"
Input "",Str0
If expr(Str0)!=6 or "6"=Str0
Then
Disp "Sorry, Try Again"
Pause 
"0"->Str0
Else
0->B
End
End
End
If expr(Str0)=6
Then
ClrHome
Disp "Congratulations!","You Beat Level","1.  Continue","With The Other","Eight Now."
End


It's untested, so I may've screwed something up, but the basic idea is use a variable to be a flag, and do

Code:
While <FlagVariable>
...
If <ConditionToExit>:Then
0->FlagVariable
End
...
End

And of course this is embeddable:

Code:
While <FlagVariable1>
...
If <ConditionToExit1>:Then
0->FlagVariable1
End
...
While <FlagVariable2> and <FlagVariable1>
...
If <ConditionToExit2>:Then
0->FlagVariable2
End
...
End
...
End
So the way that I like to approach the issue with that Menu command is as follows:


Code:
0:Menu("Menu","One",1,"Two",2,"Three",3,"Four",4
Lbl 4:1
Lbl 3:Ans+1
Lbl 2:Ans+1
Lbl 1:Ans+1->X


Once you run this, then you know that X holds the number of the label that you want. From there, you can use conditionals on X to check what to do, rather than jumping around in your program. I know that this might not be what you want, but it is a good practice that I like to use to prevent memory leaks.
merthsoft wrote:

Code:
While <FlagVariable1>
...
If <ConditionToExit1>:Then
0->FlagVariable1
End
...
While <FlagVariable2> and <FlagVariable1>
...
If <ConditionToExit2>:Then
0->FlagVariable2
End
...
End
...
End

So what does this do exactly?
With an extra variable that you change the value of when exiting loops, it's not too hard to get rid of Lbl/Goto entirely.

I generally prefer not using them in TI-BASIC, because it takes quite a while for the interpreter to reach a Lbl that is very far into the code (several seconds sometimes) compared to an If block that contains a nested loop.
So DJ, do you have any suggestions on what I could do to fix this problem, because I really want this program out soon
I know, I know, this is very old, but it still pertains to this topic. I think I may have a way around this, but I wanted to ask you guys first. Here is the code:

Code:
While theta!=1
ClrHome
Input "Must Be 2: ",A
If A=2
1->theta
End
Disp A
MrDew25 wrote:
I know, I know, this is very old, but it still pertains to this topic. I think I may have a way around this, but I wanted to ask you guys first. Here is the code:

Code:
While theta!=1
ClrHome
Input "Must Be 2: ",A
If A=2
1->theta
End
Disp A


Why would you need the input thing? I was going to suggest a workaround but I'm not actually sure what you're trying to do here (I don't quite see how this code would help with avoiding memory leaks)
Unless you're trying to do the flag variable thing mentioned earlier?
It was an example. If I were to use If statements, it would have been:

Code:
Lbl A
ClrHome
Input "Must Be 2: ",A
If A!=2
Goto A
Disp A

and that creates a memory leak. I was wondering if using a while statement fixed it
  
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 2
» 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