Omnimaga
Calculator Community => TI Calculators => Axe => Topic started by: Loaf on August 22, 2013, 05:38:12 pm
-
Noob question here. I have made a preliminary look at questions here, and could not find an answer. I also looked at the Axe Parser Documentation. Is there a way to break out of a loop? Thus far, right before my loop, I have been setting a variable to act as a boolean flag to whether the loop should continue or not. For example,
C = 1
for(T,0,10)
if(C)
code here, in the place of an actual "break" i would put "C = 0"
end:end
However, this is seems clunky and slow, because not only do I have to perform an extra boolean test, I have to finish the loop regardless of the iteration at which C is set.
Based on other questions, this appears to be the right board for this question. However, if not, I apologize in advance.
-
I'd like to say that the boolean is the only solution, you have to use EndIf to replace the End and put your condition here...
-
Would you be kind enough to give me an example of that? Does it go after a loop or after an if-statement? Practically anything would be better than the way i'm doing it now XD.
-
Unfortunately, Axe doesn't currently support continue or break statements. Fortunately, it supports gotos, which can be used to manually implement the break behavior (except in single-argument for loops) like this:
For(A,1,10)
.stuff
Goto Break
.stuff
End
Lbl Break
Continue can be implemented by simply moving the label up a line. And yes, this is the right board for the question. :)
-
Why thank you, my dear Runer. That will work perfectly for my feeble needs. :D
-
Runer, what's EndIf so? Isn't that conditionnal break?
In loops, it will exit the loop if EXP is true. But it works just like a regular End otherwise.
-
EndIf is definitely a slightly confusing command. If the condition following it is false, it acts like a normal End and execution continues at the start of the loop. If the condition is true, execution breaks out of the loop. You may notice that I used both the words break and continue to describe the behavior, because that's essentially what it is: not a conditional break, but a conditional break/continue. The two cases are either break or continue. There is no case in which execution carries on normally inside the loop, so the command marks the structural end of the loop.
EDIT: Fixed the cases being backwards. I promise they're right now.
-
Setting the counter variable to the upper limit or a value above the upper limit should make the loop stop as well, but I don't know if it works in axe.
In your code, it means that instaed of setting C to 0 to break, set T to 10 or above.
-
Okay! So The only case where that could be used is when the break condition is the last operation in the loop. That's now clear. Thanks!
-
So endif goes inside or at the end of the loop?
From what you've described, I can't see it going at the end, because that would make its 'continue' functionality useless.
-
endIf should replace the normal end
-
Endif == true => break; (stop the loop)
Endif == false => continue; ( directly go to loop begin)
(It's what I understood from Runner)
EDITED because Runer got it wrong too!
-
Endif == true => continue ( directly go to loop begin)
Endif == false => break; ( stop the loop).
(It's what I understood from Runner)
After rereading Mr. Runer's post, I am going to agree with you. Also, I now feel like I should have been able to think of AT LEAST setting the iteration variable to a terminating value to break. I'm so uncreative :(
EDIT: Ooh i'm level one now.
-
Endif == true => continue ( directly go to loop begin)
Endif == false => break; ( stop the loop).
(It's what I understood from Runner)
You know how I said EndIf is a confusing command? Well I got my description of the cases backwards... Oops :P
But you have the idea right. Your post describes exactly how the companion command End!If works. I've edited my original post to correct my mistake.
-
(Nice, it's the first step!)
If it works, you can still let it and work later on optimising. Go on and make us some good programs! ;)
i edited my precedent post, Runer. Sorry Loaf!
-
Go on and make us some good programs! ;)
I've already made a few.
Roids - your standard space shooter. However, very rudimentary and slow. Also the enemies shoot back :D
Snake - its bloody snake, what do you expect :/
TIBlocky - a port of minecraft existing in two dimensions :D With flowing water as well. Only a few types of blocks. (heavily under development)
I just needed to know if there was a method for return, because the way I was doing it before was bothering me.
-
Another thing you can do is change your For loop into a While one and put two conditions for it to end, the first one being about the For-loop-variable and the second one being about your flag.
-
By the way, Goto is perfectly fine in Axe (as opposed to TI-BASIC and just about every coding convention for any other language), so feel free to use them. The only exception is in For(#) loops where they're disallowed, as Runer mentioned.