Discuss Scratch

robert_supermario
Scratcher
52 posts

"Break" block (for getting out of loops early)

Suppose you have a loop that repeats, for example, 10 times, but you want the loop to end early if a certain condition is met, such as the space key being pressed. The break block would make it easy to do that, and would work something like this

repeat10change placeholderby45ifkey spacepressed?thenbreak

There is a bit of a workaround, where you can broadcast a message and then stop the script the loop is in when a condition is met, but I still think the block might be useful. (I don't like workarounds involving creating new messages as that can result in having many more messages than should be necessary imo)
robert_supermario
Scratcher
52 posts

"Break" block (for getting out of loops early)

Also! The workaround doesn't work with clones (because you can't broadcast a message to a clone)

Last edited by robert_supermario (Feb. 1, 2019 19:42:08)

Za-Chary
Scratcher
1000+ posts

"Break" block (for getting out of loops early)

No support.

As I have learned in other programming languages, a “break” mechanic is generally not considered very good programming practice, and there are ways to work around it.

By the way, you most definitely can broadcast messages to clones. Any broadcast received by a sprite will also be received by each of its clones.
robert_supermario
Scratcher
52 posts

"Break" block (for getting out of loops early)

Za-Chary wrote:

No support.

As I have learned in other programming languages, a “break” mechanic is generally not considered very good programming practice, and there are ways to work around it.

By the way, you most definitely can broadcast messages to clones. Any broadcast received by a sprite will also be received by each of its clones.
I didn't actually know that about clones. That's definitely very useful information, so thanks.
imfh
Scratcher
1000+ posts

"Break" block (for getting out of loops early)

Here's another workaround:

whenclickedBreakableLoop. . . This will still run once the loop stopsdefineBreakableLoopforever. . .if...thenstopthis script Does not stop the script that ran the block

Even if it isn't good practice, there are some cases where the other workarounds are more complicated than they are worth using. I wouldn't be against adding breaks, but I don't think it's really necessary either.

Also, clones are exactly like their parent. They have every script the parent has. The only difference is that clones don't run when flag clicked(they are deleted instead) and that they do run when I start as a clone.
HelmutFedder
Scratcher
2 posts

"Break" block (for getting out of loops early)

Just came accross this issue as well when starting to teach my 9-year-old programming.

In a numerics context you will sooner or later come across examples where “break” is really needed to produce readable and efficient code. There is a god reason why all “real” programing languages have “break”. As an example, take a simple prime test (taken from Wikipedia):

function is_prime(n)
if n ≤ 3 then
return n > 1
else if n mod 2 = 0 or n mod 3 = 0
return false
let i ← 5
while i × i ≤ n do
if n mod i = 0 or n mod (i + 2) = 0
return false
i ← i + 6
return true

Here is another example in a “non-numeric context”: https://softwareengineering.stackexchange.com/questions/58237/are-break-and-continue-bad-programming-practices

Of course you can ask question whether kids should learn numerics with scratch. Yes, absolutely we should use scratch to teach them numerics!

There are hundreds of brilliant numeric coding riddles for kids that are powerful. They can connect it to their math in school and they can understand how powerful computers are. You can start like this:

“Make the cat say all multipications of 7 up to 100: 7, 14, …”

“Make the cat calculate the square of a number that you enter!”

“Write a code that tests whether a number is a prime number”

“Calculate all prime number up to 10000”

… and so on.

Finally, let's look at how scratch guides you to implement breaking out of loops today.

Currently, you would use “stop this script” and introduce a custom block to make sure that “stop” effects only that part of code. This is quite a bad workaround and it is also hard to find this yourself without googleing “how to break out of loops” - so this is a workaround that kids can't find out easily. So, yes, it is a workaround, but a workaround that is hard to find is close to not existing.

So, yes, there should be “stop this loop”

Now, finally we can ask: what would be the effect of introducing this? Would a "stop “ be ”misused“ in too many other cases?

Yes, we can expect cases where a ”stop “ is used with a simple condition ”somewhere in the loop“ - which would better be implemented with a ”repeat until“ block, where the condition checking is located naturally at the beginning of the loop.

It is actually important that kids realize this. Quite generally, kids learn quickly that there are always several ways to implement things and some are easier and more readable and others are complex and error prone. They should be aware of this and learn what to use when.

This learning process is actually useful for our kids to become good programmers.

So, yes, there should be ”stop this loop" in scratch.

Last edited by HelmutFedder (Jan. 20, 2020 09:09:11)

ResExsention
New Scratcher
1000+ posts

"Break" block (for getting out of loops early)

HelmutFedder wrote:

-sniiiiip

If anything, that should be ample reasoning as to why “break” should exist.

I mean, think about it. Pretend you use a loop to generate money every second. However, this doesn't apply if you die.

Using custom blocks is as stated, a possible workaround, but it's inefficient. Not everyone wants to mess with clones or custom blocks for something that simple.
E_Equals_EmCeCube3
Scratcher
1000+ posts

"Break" block (for getting out of loops early)

It looks like this idea has been suggested before in this topic.
Please continue the discussion there so we can keep individual suggestions organized and in one topic.
Paddle2See
Scratch Team
1000+ posts

"Break" block (for getting out of loops early)

E_Equals_EmCeCube3 wrote:

It looks like this idea has been suggested before in this topic.
Please continue the discussion there so we can keep individual suggestions organized and in one topic.
Thanks for the link! It does look like this is a duplicate topic so I'll close it to keep the conversation all in one place.

Please use the existing topic in the link above.

Powered by DjangoBB