Discuss Scratch

IronBit_Studios
Scratcher
1000+ posts

Switch-block

No Support.
Zekrom01
Scratcher
1000+ posts

Switch-block

IronBit_Studios wrote:

No Support.
^ reason?

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
Scratcher
1000+ 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) 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
Post this on the Workarounds sticky

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
Scratcher
100+ posts

Switch-block

I reluctantly do it with ‘define’ and ‘if’ and ‘stop’, because it seems to be simpler than ‘if-else’
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
Scratcher
49 posts

Switch-block

Support, support, a hundred times support.
Maybe like this:
switch (var) {
value (a) {
code :: grey
}
value (b) {
code :: grey
}
default {
code :: grey
}
}
But all the red is one block, and with a + button on the outer block in between the last value and default.
100MEGAllamas
Scratcher
4 posts

Switch-block

SUPPORT
KingSkrizzle
New Scratcher
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.
Programmer_55
Scratcher
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{...}::control
It's not perfect, but It's the best I can do.
HTML-Fan
Scratcher
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
Scratcher
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
Scratcher
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:
switch{

case (my variable)::control
some code
default::control
some code
}::control
and then wonder why his or her code does not work. (Scratch does not have compiler errors like normal languages 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.
The2AndOnly
Scratcher
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:

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
Scratcher
1000+ posts

Switch-block

I made a new mockup; switch statements are really useful for avoiding clutter.

switch (variable): case [yes] {
say [Ok!]
} case [no] + - {
say [Fine then.]
} default {
say [Huh?]
}:: control
i just realized how many mockups there were

Last edited by lolecksdeehaha (March 2, 2022 01:14:36)

hiPeeps124816
Scratcher
500+ posts

Switch-block

lolecksdeehaha wrote:

I made a new mockup; switch statements are really useful for avoiding clutter.

switch (variable): case [yes] {
say [Ok!]
} case [no] + - {
say [Fine then.]
} default {
say [Huh?]
}:: control
i just realized how many mockups there were
not sure how this works, but from experience you don't know how to do something until you need to
lolecksdeehaha
Scratcher
1000+ posts

Switch-block

hiPeeps124816 wrote:

lolecksdeehaha wrote:

~snip~
not sure how this works, but from experience you don't know how to do something until you need to
Have a tutorial! Switch statements are basically if-else blocks, but less convoluted. For example:
ask [Hello there! Do you want to play the game? (y/n)] and wait
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
If the user answers “y”, this happens:
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
Scratcher
1000+ posts

Switch-block

so this:
switch (a) if (1): {...
}(add::#44bb44)(remove::#ff2222)if (2):{...
}(add::#44bb44)(remove::#ff2222)if (3):{...
}otherwise:{...}::control
or this:
if <> then{...
}(add::#44bb44)(remove::#ff2222)else if <>{...
}(add::#44bb44)(remove::#ff2222)else if <>{...
}else{...
}::control
Thingied
Scratcher
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
Scratcher
100+ posts

Switch-block

Mockup:
switch (var) {
[a] {
. . .
} (+) (- :: variables):: control stack
. . .
} :: control
Jackson49_test
Scratcher
100+ posts

Switch-block

Bump
DogCatPuppyLover
Scratcher
100+ posts

Switch-block

bump

Powered by DjangoBB