Discuss Scratch
- PH-zero
-
Scratcher
100+ posts
Switch-block
Hey 
Have you already encountered this:
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:
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.

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 cstartExept the switch-block would be one big brace rather than to seperate blocks.
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
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.
Alternative to switch:
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)
- seanbobe
-
Scratcher
500+ 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.not a bug.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 Awhen I receive [b v]
do stuff Bwhen I receive [c v]
do stuff Cwhen I receive [d v]
do stuff D
- blob8108
-
Scratcher
1000+ posts
Switch-block
There is a bug in the drop-down blocks menu, yes. But the plugin does allow it:broadcast [(letter) v] and wait(BTW there is a bug in Scratchblocks here, since the variable isn't shown as a variable.)
broadcast (letter) and wait
broadcast (letter) and waitEDIT: Anyway, have a mockup of a “switch”, just for fun:
switch (level) :: control cstartBut really we want the cases embedded in the switch somehow:
case [1] :: control cstart
end
case [2] :: control cstart
end
case [3] :: control cstart
end
default :: control cstart
end
end
switch (level) -- case [1] :: control cstartAnd the wording could be improved, too:
case [2] :: celse
case [3] :: celse
default :: celse
end
if (level) is [1] then :: control cstart…but then this does make one question why we don't just have “else if” instead.
else if it's [2] then :: celse
else if it's [3] then :: celse
else :: celse
end
Last edited by blob8108 (March 1, 2014 21:31:11)
- davidkt
-
Scratcher
1000+ posts
Switch-block
Mockup:
switch (variable) ::control // pretend it's joined with the block belowAnyway, support.
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
- Firedrake969
-
Scratcher
1000+ posts
Switch-block
Mockup:Would we want optional breaks or not?switch (variable) ::control // pretend it's joined with the block belowAnyway, support.
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
- davidkt
-
Scratcher
1000+ posts
Switch-block
Not. It would be too confusing, and there aren't going to be many circumstances where you'll need to not have the break.Mockup:Would we want optional breaks or not?switch (variable) ::control // pretend it's joined with the block belowAnyway, support.
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
- A-no-meep
-
Scratcher
100+ posts
Switch-block
Agreed. Most instances of deliberately not having break can be solved with duplicate.Not. It would be too confusing, and there aren't going to be many circumstances where you'll need to not have the break.Mockup:Would we want optional breaks or not?switch (variable) ::control // pretend it's joined with the block belowAnyway, support.
case (1)::cstart control
...
case (2)::celse
...
case (3) + - ::celse
...
default ::celse
...
end
- 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
Umm … couldn't you just nest some If's? …You could, but it gets annoying when you have to do 100 ifs like:if () then
if <> then
end
if <> then
end
end
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.
- Scratcher1002
-
Scratcher
1000+ posts
Switch-block
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
I know what they are in PHP now.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
- 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.


thanks mobluse











