Skip to first unread message

Mayank Rathore

unread,
Jul 20, 2017, 3:05:37 PM7/20/17
to MIT App Inventor Forum
How to break out of current while loop in AI2? Please help!

Tim Carter

unread,
Jul 20, 2017, 4:30:42 PM7/20/17
to mitappinv...@googlegroups.com
Just like you would in any coding situation, set an if/then clause where the loop meets a certain condition

see here:

SteveJG

unread,
Jul 21, 2017, 8:44:23 AM7/21/17
to MIT App Inventor Forum
You  cannot use a Button.Click or other method to break out of a loop .  To interrupt the loop (terminating the looping before it self terminates) must be done programmably, as AI doesn't have a built in loop break.  See Tim's post   and here https://code.google.com/archive/p/app-inventor-for-android/issues/474 .    And endless loop will result in a run time error.  The model of event processing in App Inventor by  Lyn  
 Depending on how you are iterating,, instead of a simple loop you could try using a Clock as suggested by Taifun  The 'Do something' example .  You did not provide an example of the 'looping' activity, so that is a guess.

Issue 474 (to provide a Break function) has been on MIT's 'to do' list since late 2010 ... almost seven years.  This is probably insolvable because AI executes asynchronously.

A runaway activity within a loop will crash your app.   What to do?  Certainly avoid long loops (and perhaps loops in general). Never use a loop without a way to escape.

Regards,
Steve


Taifun

unread,
Jul 21, 2017, 9:30:54 AM7/21/17
to MIT App Inventor Forum
@SteveJG: Scott found a solution, see the link Tim posted...
Taifun



SteveJG

unread,
Jul 21, 2017, 9:46:55 AM7/21/17
to MIT App Inventor Forum
@Thanks Taifun,.  Scott's examples  work to 'terminate a loop early' if the loop is  processing a List.  The methods would not terminate  Math activity.   But, good to know.

Can something similar be done with 

to terminate before the counter reaches 1000 using perhaps a button click?  The only solution I can find other than providing an if statement within the loop..say if counter= 200 then counter =1001  which is a software 'termination'    is to use something once suggested by ABG using a Clock  and that 'works sometimes".


--Steve

Abraham Getzler

unread,
Jul 21, 2017, 9:57:03 AM7/21/17
to MIT App Inventor Forum
This is an example of forced breaks in sieve based prime number search ...

ABG

Reply all
Reply to author
Forward
0 new messages