# Omnimaga

## Calculator Community => TI Calculators => Axe => Topic started by: Loaf on August 22, 2013, 05:38:12 pm

Title: break
Post 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.
Title: Re: break
Post by: Eiyeron on August 22, 2013, 05:43:12 pm
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...
Title: Re: break
Post by: Loaf on August 22, 2013, 05:53:33 pm
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.
Title: Re: break
Post by: Runer112 on August 22, 2013, 05:54:16 pm
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. :)
Title: Re: break
Post by: Loaf on August 22, 2013, 05:56:32 pm
Why thank you, my dear Runer. That will work perfectly for my feeble needs. :D
Title: Re: break
Post by: Eiyeron on August 22, 2013, 05:57:55 pm
Runer, what's EndIf so? Isn't that conditionnal break?
Quote from: command list
In loops, it will exit the loop if EXP is true. But it works just like a regular End otherwise.
Title: Re: break
Post by: Runer112 on August 22, 2013, 06:05:17 pm
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.
Title: Re: break
Post by: ben_g on August 22, 2013, 06:08:07 pm
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.
Title: Re: break
Post by: Eiyeron on August 22, 2013, 06:09:16 pm
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!
Title: Re: break
Post by: Loaf on August 22, 2013, 06:09:18 pm
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.
Title: Re: break
Post by: ben_g on August 22, 2013, 06:10:12 pm
endIf should replace the normal end
Title: Re: break
Post by: Eiyeron on August 22, 2013, 06:11:25 pm
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!
Title: Re: break
Post by: Loaf on August 22, 2013, 06:13:36 pm
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.
Title: Re: break
Post by: Runer112 on August 22, 2013, 06:16:30 pm
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.
Title: Re: break
Post by: Eiyeron on August 22, 2013, 06:18:42 pm
(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!
Title: Re: break
Post by: Loaf on August 22, 2013, 06:23:34 pm
Go on and make us some good programs! ;)