This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-02-12 08:47:48

Kowalskz
New Scratcher
Registered: 2010-11-20
Posts: 20

How to exit out of a loop prematurely?

Hi,

Is there a way to get out of a Repeat Until or Forever If loop prematurely? 

I am looking for something similar to the Exit command in some other programming languages.

Thanks.

Offline

 

#2 2011-02-12 08:50:09

Lar-Rew
Scratcher
Registered: 2010-02-19
Posts: 100+

Re: How to exit out of a loop prematurely?

You can use this block to exit a script prematurely:

[blocks]<stop script>[/blocks]

Hope that helps!

Offline

 

#3 2011-02-12 09:08:33

mathematics
Scratcher
Registered: 2009-03-01
Posts: 1000+

Re: How to exit out of a loop prematurely?

There isn't such command in Scratch yet. However, it has already been suggested.

<stop script>[/blocks] stops the whole script, so...
- always works*:

<forever if>[/blocks] and <if>[/blocks].
- only works* when there is nothing under it:

<repeat( [/blocks] and <repeat until>[/blocks]

* "works" means "works like the Exit command".

Last edited by mathematics (2011-02-12 09:11:04)

Offline

 

#4 2011-02-12 13:56:25

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How to exit out of a loop prematurely?

mathematics wrote:

There isn't such command in Scratch yet. However, it has already been suggested.

<stop script>[/blocks] stops the whole script, so...
- always works*:

<forever if>[/blocks] and <if>[/blocks].
- only works* when there is nothing under it:

<repeat( [/blocks] and <repeat until>[/blocks]

* "works" means "works like the Exit command".

Thanks for referring to my suggestion.  big_smile  I would really like it to happen.

Offline

 

#5 2011-02-14 20:58:47

Kowalskz
New Scratcher
Registered: 2010-11-20
Posts: 20

Re: How to exit out of a loop prematurely?

Thanks guys.

I knew about the Stop Script command. However, I can't use it in this case as I need to continue executing the the code after exiting the loop.

mathematics wrote:

There isn't such command in Scratch yet. However, it has already been suggested.

<stop script>[/blocks] stops the whole script, so...
- always works*:

<forever if>[/blocks] and <if>[/blocks].
- only works* when there is nothing under it:

<repeat( [/blocks] and <repeat until>[/blocks]

* "works" means "works like the Exit command".

I don't understand this. These blocks look confusing.  Can you elaborate on that? Thanks.

Offline

 

#6 2011-02-14 21:12:04

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How to exit out of a loop prematurely?

Just add a:
<<  <and> <( <{ break }> <=> 0 )> >>

and just set break to 1 to break the loop.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#7 2011-02-15 05:32:58

mathematics
Scratcher
Registered: 2009-03-01
Posts: 1000+

Re: How to exit out of a loop prematurely?

There's no way to exit the loop prematurely then...

Offline

 

#8 2011-02-15 06:39:51

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: How to exit out of a loop prematurely?

There IS!

Code:

[set [stop] to (1)]
< <repeat clause> and <(stop) = (0)> >

if <premature exit clause>

[set [stop] to (0)]

else

[set [stop] to (1)]

[coding]

and something similar can be done for repeat ().

Last edited by Hardmath123 (2011-02-15 06:40:58)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#9 2011-02-15 16:20:48

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How to exit out of a loop prematurely?

Hardmath123 wrote:

There IS!

Code:

[set [stop] to (1)]
< <repeat clause> and <(stop) = (0)> >

if <premature exit clause>

[set [stop] to (0)]

else

[set [stop] to (1)]

[coding]

and something similar can be done for repeat ().

That's what I said... or tried to say anyway.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#10 2011-02-16 06:46:56

mathematics
Scratcher
Registered: 2009-03-01
Posts: 1000+

Re: How to exit out of a loop prematurely?

However, I don't get what you guys are saying...

Offline

 

#11 2011-02-16 18:05:52

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How to exit out of a loop prematurely?

Basically make one of the conditions of the loop: Variable == 1

To break the loop, set Variable to 0.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

Board footer