Discuss Scratch

theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

I suggest a block to break out of loops, like in many other programming languages. In another programming language, it might look like this (pseudo-code):

var i = 0;
foreach person as name {
if name == "John" {
break;
}
i++;
}

Then you would have the index of “John” stored in the variable i. Basically, a break statement exits a loop.

In Scratch I think it could look like:

set [i v] to [0]
repeat (length of [list v])
if <(item (i) of [list v]) = [John]> then
exit this loop :: control cap
end
change [i v] by [1]
end
say (i) // shows the index of John in list

Of course, this is just an example, and I know that there are many work-arounds, but I think it is just an extremely useful feature to have, and (pretty much) every other programming feature implements this statement, so by including it Scratch would introduce another feature central to other languages, which is the point of Scratch anyway.

I used the name “exit this loop” because the name break might have been confusing to new Scratchers. See here for a list of all possible names, and suggest your own: http://scratch.mit.edu.ezproxyberklee.flo.org/discuss/topic/53810/?page=1#post-453542

It could also be an extra option in the stop () block, see the link for design ideas.
stop [this loop v] :: control cap

Last edited by theonlygusti (Sept. 2, 2015 15:28:53)

Firedrake969
Scratcher
1000+ posts

New Block to act like a break statement

Support.
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

Firedrake969 wrote:

Support.
2nd time you've supported a work-aroundable thing ?
stickfiregames
Scratcher
1000+ posts

New Block to act like a break statement

Support, but the name “break” might confuse new Scratchers. These might be better:
exit this loop :: control
skip to end of loop :: control
exit repeat :: control // repeat and repeat until are the only loops this needs to be used in, forever can use stop script.

Last edited by stickfiregames (Sept. 28, 2014 14:19:04)

theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

stickfiregames wrote:

Support, but the name “break” might confuse new Scratchers. These might be better:
exit this loop :: control
skip to end of loop :: control
Good suggestion. I'll add a new name to the top, and create a new post with naming suggestions. Thanks
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

exit this loop :: control stack

exit this loop :: control cap // cap design, might be more fitting as it's the "end" of a loop

break :: control stack

break :: control cap

stop loop :: control stack

stop loop :: control cap

leave this loop :: control stack

leave this loop :: control cap

Leave your own naming suggestions, and whether you think they should be cap or stack blocks.

So far more support has been shown towards cap blocks. I will update all my ideas.

Maybe we could add a drop down to the "stop " block:

stop [this loop v]

Last edited by theonlygusti (Sept. 28, 2014 15:42:26)

stickfiregames
Scratcher
1000+ posts

New Block to act like a break statement

theonlygusti wrote:

exit this loop :: control stack

exit this loop :: control cap // cap design, might be more fitting as it's the "end" of a loop

break :: control stack

break :: control cap

stop loop :: control stack

stop loop :: control cap

leave this loop :: control stack

leave this loop :: control cap

Leave your own naming suggestions, and whether you think they should be cap or stack blocks.
Caps make more sense.
Centrifugal
Scratcher
500+ posts

New Block to act like a break statement

Support
MegaApuTurkUltra
Scratcher
1000+ posts

New Block to act like a break statement

Support but there is a workaround. Put your loop in a custom block and then do
define DoLoop
set [i v] to [0]
repeat (length of [list v])
if <(item (i) of [list v]) = [John]> then
stop [this script v]
end
change [i v] by [1]
end

when gf clicked
DoLoop
say (i) // shows the index of John in list
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

MegaApuTurkUltra wrote:

Support but there is a workaround. Put your loop in a custom block and then do
...
I know that there are several workarounds, I said so in the original post. Thanks for the support anyhow!

Last edited by theonlygusti (Sept. 28, 2014 15:34:46)

Blueinkproductions
Scratcher
1000+ posts

New Block to act like a break statement

when green flag clicked
set [i v] to [0 ]
repeat until <[2] = [ 1]>
say [Support!] for (1) secs
change [i v] by (1)
if <(i) > [1000 ]> then
exit this loop :: control cap
end

end

theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

Blueinkproductions wrote:

when green flag clicked
set [i v] to [0 ]
repeat until <[2] = [ 1]>
say [Support!] for (1) secs
change [i v] by (1)
if <(i) > [1000 ]> then
exit this loop :: control cap
end

end

XD
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

when green flag clicked
set [i v] to [0 ]
repeat until <[2] = [ 1]>
say [Support!] for (1) secs
change [i v] by (1)
if <(i) > [1000 ]> then
exit this loop :: control cap
end
end
say [i = 1001, this is awesome!]


ProdigyZeta7
Scratcher
1000+ posts

New Block to act like a break statement

Support. Regardless of a workaround I find it tedious to make as few scripts/variables while escaping loops.
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

Bump!
Tropic
Scratcher
1000+ posts

New Block to act like a break statement

Isn't foreach from Java?

(Support, because I like Java and maybe I can get more used to Java if this gets implemented )
Sonickyle
Scratcher
1000+ posts

New Block to act like a break statement

Support.
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

Tropic wrote:

Isn't foreach from Java?

(Support, because I like Java and maybe I can get more used to Java if this gets implemented )
foreach is from a load of languages, php, java, …
Tropic
Scratcher
1000+ posts

New Block to act like a break statement

theonlygusti wrote:

Tropic wrote:

Isn't foreach from Java?

(Support, because I like Java and maybe I can get more used to Java if this gets implemented )
foreach is from a load of languages, php, java, …
Oh, ok
theonlygusti
Scratcher
1000+ posts

New Block to act like a break statement

Tropic, have you been on for the past 10 hours? Non-stop?

Powered by DjangoBB