Discuss Scratch
- Zekrom01
-
1000+ posts
Switch-block
^ reason? No Support.

Anyway, no support due to this workaround:
hat block here ::extension hat
scripts here ::grey
broadcast (letter) and wait //if "a" is the letter, "a" is broadcasted and so on
scripts here ::grey
when I receive [a v]
scripts here ::grey
when I receive [b v]
scripts here ::grey
- MathlyCat
-
1000+ posts
Switch-block
Post this on the Workarounds sticky 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 Awhen I receive [b v]
do stuff Bwhen I receive [c v]
do stuff Cwhen I receive [d v]
do stuff D

SUPPORT!!!
I have to do this all the time, it actually restricts me from a lot of my ideas because it's just too tedious!
- inotomoking
-
100+ posts
Switch-block
I reluctantly do it with ‘define’ and ‘if’ and ‘stop’, because it seems to be simpler than ‘if-else’
Anyway, I need ‘switch’!
define (x)
if <[x] = [a]> then
do stuff A
stop [this script]
end
if <[x] = [b]> then
do stuff B
stop [this script]
end
if <[x] = [c]> then
do stuff C
stop [this script]
end
Anyway, I need ‘switch’!
- OldBunny2800
-
49 posts
Switch-block
Support, support, a hundred times support.
Maybe like this:
Maybe like this:
switch (var) {But all the red is one block, and with a + button on the outer block in between the last value and default.
value (a) {
code :: grey
}
value (b) {
code :: grey
}
default {
code :: grey
}
}
- KingSkrizzle
-
11 posts
Switch-block
SUPPORT a million times. I'm sick of having to nest If statements to replicate this functionality. Case statements are a basic part of every programming language I've ever seen, and they're not difficult to understand.
Skrizzle drops the mic.
Skrizzle drops the mic.
- Programmer_55
-
51 posts
Switch-block
SUPPORT. All of these workarounds are ugly, and this situation comes up a lot. I think a better mockup is in order though.
switch value:[]if[]then{...}if[]then{...}if[]then{...}@delInput@addInput else{...}::controlIt's not perfect, but It's the best I can do.
- HTML-Fan
-
1000+ posts
Switch-block
I don't think that it's needed an awful lot, but it would be nice to have. But now really, it could actually be confusing to newbies.
- jharris1952
-
19 posts
Switch-block
Absolutely Support!! If we had this then advanced projects would be so much easier!! Maybe make a tutorial for it and have it the use of the block in your own projects be unlocked after watching the tutorial, so people would not get confused.
- Greg8128
-
500+ posts
Switch-block
No support. Here's how switch statements work and why they wouldn't fit Scratch:
A switch statement is NOT a chain of if-else statements.
An if-else statement decides between two branches using a yes-or-no question.
A switch statement, on the other hand, looks up which branch to take using a map data structure. This is faster, but also means that the map must be generated before the switch statement is entered for the first time.
This causes two problems:
1. A Scratcher might try to do:
2. Depending on how the Scratch VM currently works this might be impossible to do. In which case you would have to rebuild the dictionary every time the loop is entered which destroys the efficiency gains that you get from the switch statement.
There's also the matter of how the ‘case _:’ and ‘break’ syntax which is used in switch statements would require blocks that can only be placed directly inside switch statements (or repeat loops as well in the case of the break block), as placing such a block anywhere else is nonsense that should cause a compiler error to be thrown. Scratch does not have compiler errors of course, so whatever.
A switch statement is NOT a chain of if-else statements.
An if-else statement decides between two branches using a yes-or-no question.
A switch statement, on the other hand, looks up which branch to take using a map data structure. This is faster, but also means that the map must be generated before the switch statement is entered for the first time.
This causes two problems:
1. A Scratcher might try to do:
switch{and then wonder why his or her code does not work. (Scratch does not have compiler errors like normal languages do)
case (my variable)::control
some code
default::control
some code
}::control
2. Depending on how the Scratch VM currently works this might be impossible to do. In which case you would have to rebuild the dictionary every time the loop is entered which destroys the efficiency gains that you get from the switch statement.
There's also the matter of how the ‘case _:’ and ‘break’ syntax which is used in switch statements would require blocks that can only be placed directly inside switch statements (or repeat loops as well in the case of the break block), as placing such a block anywhere else is nonsense that should cause a compiler error to be thrown. Scratch does not have compiler errors of course, so whatever.
- The2AndOnly
-
88 posts
Switch-block
I had a hard time learning switch because it wasn't used in scratch. I haven't ever encountered a scenario where I needed to use this, but this is what it might look like:
I don't support because it would be too complicated for scratch and it's a lot easier to add if else statements. If the switch block was a thing then scratch would need a way to customize the block to add more cases, which gets complicated.
switch (letter) default: {
...
} case1: {
...
} case 2: {
...
} :: control
I don't support because it would be too complicated for scratch and it's a lot easier to add if else statements. If the switch block was a thing then scratch would need a way to customize the block to add more cases, which gets complicated.
- lolecksdeehaha
-
1000+ posts
Switch-block
I made a new mockup; switch statements are really useful for avoiding clutter.
switch (variable): case [yes] {i just realized how many mockups there were
say [Ok!]
} case [no] + - {
say [Fine then.]
} default {
say [Huh?]
}:: control
Last edited by lolecksdeehaha (March 2, 2022 01:14:36)
- hiPeeps124816
-
500+ posts
Switch-block
really useful for avoiding clutter.not sure how this works, but from experience you don't know how to do something until you need to I made a new mockup; switch statements areswitch (variable): case [yes] {i just realized how many mockups there were
say [Ok!]
} case [no] + - {
say [Fine then.]
} default {
say [Huh?]
}:: control
- lolecksdeehaha
-
1000+ posts
Switch-block
Have a tutorial! Switch statements are basically if-else blocks, but less convoluted. For example:not sure how this works, but from experience you don't know how to do something until you need to ~snip~
ask [Hello there! Do you want to play the game? (y/n)] and waitIf the user answers “y”, this happens:
switch (answer): case [y] {
say [Ok! Starting the game up...]
start up the game :: grey
} case [n] - {
say [Ok! Bye!]
...
} case [y/n] + - {
say [That's not what I meant, silly!]
} default {
say [Huh? Please say y/n.]
}:: control
say [Ok! Starting the game up...]
start up the game :: grey
If the user answers “n”, this happens:
say [Ok! Bye!]
... :: grey
If the user answers “y/n”, this happens:
say [That's not what I meant, silly!]
And if the user answers anything else that's not there, this happens:
say [Huh? Please say y/n.]
- ideapad-320
-
1000+ posts
Switch-block
so this:
switch (a) if (1): {...or this:
}(add::#44bb44)(remove::#ff2222)if (2):{...
}(add::#44bb44)(remove::#ff2222)if (3):{...
}otherwise:{...}::control
if <> then{...
}(add::#44bb44)(remove::#ff2222)else if <>{...
}(add::#44bb44)(remove::#ff2222)else if <>{...
}else{...
}::control
- Thingied
-
1000+ posts
Switch-block
A big problem I'm having with these mockups are the buttons. No other block has a button on it which can be confusing.
- SuperMarioHome
-
100+ posts
Switch-block
Mockup:
switch (var) {
[a] {
. . .
} (+) (- :: variables):: control stack
. . .
} :: control