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)


Ever wanted to rotate the stage? Or to
go to x:() y:() z:() ::custom
Click me!
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

Ever wanted to rotate the stage? Or to
go to x:() y:() z:() ::custom
Click me!
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.

Sadly my kumquat was eaten by an evil signature.
_______________
|My internets.…..|
\Kumquat Shield/
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)


tosh · slowly becoming a grown-up adult and very confused about it
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.

Remember when I looked like this? I still do.


Float, my Scratch 2.0 mod | My (somewhat under-construction) blog
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?

'17 rickoid

bf97b44a7fbd33db070f6ade2b7dc549
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.

Remember when I looked like this? I still do.


Float, my Scratch 2.0 mod | My (somewhat under-construction) blog
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.

[ v]
A dropdown to nowhere
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.

bottom text
Le_adrien
Scratcher
80 posts

Switch-block

Support!

I have no ideas from signature :S
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.

bottom text
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.

$(".box-head")[0].textContent = "committing AT crimes since $whenever"
Lax125
Scratcher
14 posts

Switch-block

Support for switch statement blocks; I have messy nested if/else's in my script.

when green flag clicked
forever

say [Help, I'm stuck in an infinite loop!]

end

Powered by DjangoBB