Discuss Scratch

PH-zero
Scratcher
100+ posts

Switch-block

Hey

Have you already encountered this:

if <(letter) = [a]>
do stuff
end
if <(letter) = [b]>
do stuff
end
if <(letter) = [c]>
do stuff
end
if <(letter) = [d]>
do stuff
end
if <(letter) = [e]>
do stuff
end

In most of all programming-languages there is a “switch” statement.
In scratch The implementation might be difficult…
but i could imagine something like this:

switch (letter) ::control cstart
if [a] then ::cstart
do stuff
if [b] then ::celse
do stuff
if [c] then ::celse
do stuff
if [d] then ::celse
do stuff
end::cend
end::cend
Exept the switch-block would be one big brace rather than to seperate blocks.
This is just an example of how a switch block could be realized.
But it definetly can be desinged in a way that new scratchers don't get confused.

Last edited by PH-zero (Aug. 12, 2014 18:31:30)

mobluse
Scratcher
100+ posts

Switch-block

I usually do it with if else because I think it's faster, but there is a workaround for switch using broadcast, see below.

if <[a] = (letter)> then
do stuff A
else
if <[b] = (letter)> then
do stuff B
else
if <[c] = (letter)> then
do stuff C
else
if <[d] = (letter)> then
do stuff D
end
end
end
end

Alternative to switch:
broadcast (letter) and wait
(Edit: fixed looks of variable.)
when I receive [a v]
do stuff A
when I receive [b v]
do stuff B
when I receive [c v]
do stuff C
when I receive [d v]
do stuff D

Last edited by mobluse (March 8, 2014 23:03:07)

PH-zero
Scratcher
100+ posts

Switch-block

Wow not bad! thanks mobluse
seanbobe
Scratcher
500+ posts

Switch-block

mobluse wrote:

I usually do it with if else because I think it's faster, but there is a workaround for switch using broadcast, see below.

if <[a] = (letter)> then
do stuff A
else
if <[b] = (letter)> then
do stuff B
else
if <[c] = (letter)> then
do stuff C
else
if <[d] = (letter)> then
do stuff D
end
end
end
end

Alternative to switch:
broadcast [(letter) v] and wait
(BTW there is a bug in Scratchblocks here, since the variable isn't shown as a variable.)
when I receive [a v]
do stuff A
when I receive [b v]
do stuff B
when I receive [c v]
do stuff C
when I receive [d v]
do stuff D
not a bug.
blob8108
Scratcher
1000+ posts

Switch-block

mobluse wrote:

broadcast [(letter) v] and wait
(BTW there is a bug in Scratchblocks here, since the variable isn't shown as a variable.)
There is a bug in the drop-down blocks menu, yes. But the plugin does allow it:
broadcast (letter) and wait
broadcast (letter) and wait
EDIT: Anyway, have a mockup of a “switch”, just for fun:
switch (level) :: control cstart
case [1] :: control cstart
end
case [2] :: control cstart
end
case [3] :: control cstart
end
default :: control cstart
end
end
But really we want the cases embedded in the switch somehow:
switch (level) -- case [1] :: control cstart
case [2] :: celse
case [3] :: celse
default :: celse
end
And the wording could be improved, too:
if (level) is [1] then :: control cstart
else if it's [2] then :: celse
else if it's [3] then :: celse
else :: celse
end
…but then this does make one question why we don't just have “else if” instead.

Last edited by blob8108 (March 1, 2014 21:31:11)

Mickey_Fan
Scratcher
100+ posts

Switch-block

Support.
chennes
Scratcher
1 post

Switch-block

Support.
davidkt
Scratcher
1000+ posts

Switch-block

Mockup:
switch (variable) ::control // pretend it's joined with the block below
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
Anyway, support.
Firedrake969
Scratcher
1000+ posts

Switch-block

davidkt wrote:

Mockup:
switch (variable) ::control // pretend it's joined with the block below
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
Anyway, support.
Would we want optional breaks or not?
davidkt
Scratcher
1000+ posts

Switch-block

Firedrake969 wrote:

davidkt wrote:

Mockup:
switch (variable) ::control // pretend it's joined with the block below
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
Anyway, support.
Would we want optional breaks or not?
Not. It would be too confusing, and there aren't going to be many circumstances where you'll need to not have the break.
A-no-meep
Scratcher
100+ posts

Switch-block

davidkt wrote:

Firedrake969 wrote:

davidkt wrote:

Mockup:
switch (variable) ::control // pretend it's joined with the block below
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
Anyway, support.
Would we want optional breaks or not?
Not. It would be too confusing, and there aren't going to be many circumstances where you'll need to not have the break.
Agreed. Most instances of deliberately not having break can be solved with duplicate.
MabonBaladevaKain
Scratcher
100+ posts

Switch-block

Umm … couldn't you just nest some If's? …
if () then

if <> then

end
if <> then

end

end
Scratcher1002
Scratcher
1000+ posts

Switch-block

MabonBaladevaKain wrote:

Umm … couldn't you just nest some If's? …
if () then

if <> then

end
if <> then

end

end
You could, but it gets annoying when you have to do 100 ifs like:
if <(pick random (1) to (100)) = (x)> then
blah
end
and so on
and so forth
CatsUnited
Scratcher
1000+ posts

Switch-block

What is a switch statement? I haven't learnt enough of any programming language to know what it is.
Le_adrien
Scratcher
80 posts

Switch-block

Support!
Scratcher1002
Scratcher
1000+ posts

Switch-block

CatsUnited wrote:

What is a switch statement? I haven't learnt enough of any programming language to know what it is.
Normally it's an if block with many cases, but one variable like this:

Javascript:

switch (variable)
case 1

break
case 2

break
default
CatsUnited
Scratcher
1000+ posts

Switch-block

Scratcher1002 wrote:

CatsUnited wrote:

What is a switch statement? I haven't learnt enough of any programming language to know what it is.
Normally it's an if block with many cases, but one variable like this:

Javascript:

switch (variable)
case 1

break
case 2

break
default
I know what they are in PHP now.
MegaApuTurkUltra
Scratcher
1000+ posts

Switch-block

I support. Not only would this make code less cluttered, but if would also make execution faster as the Scratch interpreter would have an opportunity to use a switch table or something for optimization.
Lax125
Scratcher
14 posts

Switch-block

Support for switch statement blocks; I have messy nested if/else's in my script.
Scratcher1002
Scratcher
1000+ posts

Switch-block

So much support btw.

Powered by DjangoBB