Discuss Scratch

PH-zero
Scratcher
100+ posts

Switch-block

Hey

Have you already encountered this:

ifletter=adostuffifletter=bdostuffifletter=cdostuffifletter=ddostuffifletter=edostuff

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:

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

ifa=letterthendostuffAelseifb=letterthendostuffBelseifc=letterthendostuffCelseifd=letterthendostuffD

Alternative to switch:
broadcastletterandwait
(Edit: fixed looks of variable.)
whenIreceiveadostuffA
whenIreceivebdostuffB
whenIreceivecdostuffC
whenIreceiveddostuffD

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.

ifa=letterthendostuffAelseifb=letterthendostuffBelseifc=letterthendostuffCelseifd=letterthendostuffD

Alternative to switch:
broadcast(letter)andwait
(BTW there is a bug in Scratchblocks here, since the variable isn't shown as a variable.)
whenIreceiveadostuffA
whenIreceivebdostuffB
whenIreceivecdostuffC
whenIreceiveddostuffD
not a bug.
blob8108
Scratcher
1000+ posts

Switch-block

mobluse wrote:

broadcast(letter)andwait
(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
broadcastletterandwait
EDIT: Anyway, have a mockup of a “switch”, just for fun:
switchlevelcase1endcase2endcase3enddefaultendend
But really we want the cases embedded in the switch somehow:
switchlevel--case1case2case3defaultend
And the wording could be improved, too:
iflevelis1thenelseifit's2thenelseifit's3thenelseend
…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:
switchvariable pretend it's joined with the block belowcase1. . .case2. . .case3+-. . .default. . .end
Anyway, support.
Firedrake969
Scratcher
1000+ posts

Switch-block

davidkt wrote:

Mockup:
switchvariable pretend it's joined with the block belowcase1. . .case2. . .case3+-. . .default. . .end
Anyway, support.
Would we want optional breaks or not?
davidkt
Scratcher
1000+ posts

Switch-block

Firedrake969 wrote:

davidkt wrote:

Mockup:
switchvariable pretend it's joined with the block belowcase1. . .case2. . .case3+-. . .default. . .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:
switchvariable pretend it's joined with the block belowcase1. . .case2. . .case3+-. . .default. . .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? …
ifthenifthenifthen
Scratcher1002
Scratcher
1000+ posts

Switch-block

MabonBaladevaKain wrote:

Umm … couldn't you just nest some If's? …
ifthenifthenifthen
You could, but it gets annoying when you have to do 100 ifs like:
ifpickrandom1to100=xthenblahandsoonandsoforth
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