Discuss Scratch
- FiveCubed
- Scratcher
100+ posts
switch / case block
This block would be the alternative to 20 if blocks. You would input a variable, somethings the variable could be, and if it equals one of those, then it will run the code underneath. And if none of these activate, the default one at the bottom activates. like this,
but in one block.
when green flag clicked
if <(color) = [green]> then
do something
end
if <(color) = [red]> then
do something
end
if <(color) = [blue]> then
do something
end
if <(color) = [orange]> then
do something
end
if <not <<<<green> or <red>> or <blue>> or <orange>>> then
do something default
end
but in one block.
- gdpr533f604550b2f20900645890
- Scratcher
1000+ posts
switch / case block
Will a break statement be necessary, or would Scratch automatically leave the switch statement once the value's corresponding statements are executed? I'm afraid that needing a break statement to exit an option would confuse beginners, and I don't understand the design and reasoning behind it myself.
- FiveCubed
- Scratcher
100+ posts
switch / case block
No, it wouldn't be necessary at all to have a break statement. Scratch can just have it act like a bunch of if statements put together. The reasoning behind this is that I tend to make scratch lag with the large amounts of code I make it render. This would reduce lag and be convenient. Will a break statement be necessary, or would Scratch automatically leave the switch statement once the value's corresponding statements are executed? I'm afraid that needing a break statement to exit an option would confuse beginners, and I don't understand the design and reasoning behind it myself.
- Sheep_tester
- Scratcher
500+ posts
switch / case block
I believe that this isn't exactly the workaround for the switch statement:
But yeah, support.
because if I did something such aswhen green flag clicked
if <(color) = [green]> then
do something
end
if <(color) = [red]> then
do something
end
if <(color) = [blue]> then
do something
end
if <(color) = [orange]> then
do something
end
if <not <<<<green> or <red>> or <blue>> or <orange>>> then
do something default
end
if <(color) = [blue]> thenall the if blocks would run because the variable “color” is being changed.
set [color v] to [orange]
end
if <(color) = [orange]> then
set [color v] to [purple]
end
if <not<<(color) = [blue]>or<(color) = [orange]>> then
set [color v] to [blue]
end
But yeah, support.
- gdpr533f604550b2f20900645890
- Scratcher
1000+ posts
switch / case block
FiveCubed's workaround is correct, because the switch construct needs a break statement or else it will continue to the next case. I believe that this isn't exactly the workaround for the switch statement:
-snip-
because if I did something such asif <(color) = [blue]> thenall the if blocks would run because the variable “color” is being changed.
set [color v] to [orange]
end
if <(color) = [orange]> then
set [color v] to [purple]
end
if <not<<(color) = [blue]>or<(color) = [orange]>> then
set [color v] to [blue]
end
But yeah, support.
public class test { public static void main(String[] args) { int n = 0; switch (n) { case 0: n = 1; case 1: System.out.println(n); } } }
For a workaround for a switch statement with break statements, use nested if-else statements instead.
- MrFluffyPenguins
- Scratcher
1000+ posts
switch / case block
just ignore this post i made, move along, nothing to see here
Last edited by MrFluffyPenguins (Sept. 10, 2020 14:25:28)
- Kami-ko
- Scratcher
100+ posts
switch / case block
support, nested if/else/if/else statements can get pretty annoying and it's not very optimal
- fdreerf
- Scratcher
1000+ posts
switch / case block
You are thinking wrong then.I'm startig to think your @topicbumper BUMP!
This block is a little clunky to make, but I tried my best:
switch (n) {
case (0) {
... ::list
} case (1) {
... ::events
} case (2) {
... ::variables
} case (3) {
... ::control
} case (4) {
... ::operators
} @delInput @addInput ::control
} ::control
- PGBFLITE6373
- Scratcher
1000+ posts
switch / case block
No i think its there alt also imma get back on topicYou are thinking wrong then.I'm startig to think your @topicbumper BUMP!
Thank god you closed your scratchblock tags
- PkmnQ
- Scratcher
1000+ posts
switch / case block
It's literally fdreerf's alt.No i think its there alt also imma get back on topicYou are thinking wrong then.I'm startig to think your @topicbumper BUMP!
Thank god you closed your scratchblock tags
Anyway, it would be weird, but if they manage to implement it sensibly somehow, then support.
- PGBFLITE6373
- Scratcher
1000+ posts
switch / case block
imma make an alt and replyIt's literally fdreerf's alt.No i think its there alt also imma get back on topicYou are thinking wrong then.I'm startig to think your @topicbumper BUMP!
Thank god you closed your scratchblock tags
Anyway, it would be weird, but if they manage to implement it sensibly somehow, then support.
- sathvikrias
- Scratcher
500+ posts
switch / case block
no i'm not topicbumper's alt . No i think its there alt
this suggestion would need varadic inputs, aka @addInput and @delInput in scratchblocks.
- PGBFLITE6373
- Scratcher
1000+ posts
switch / case block
yes i know that your not, but you seem like it. other then that support has less clutterno i'm not topicbumper's alt . No i think its there alt
this suggestion would need varadic inputs, aka @addInput and @delInput in scratchblocks.
- plqaokwsijeduhrfyg
- Scratcher
500+ posts
switch / case block
OH MY GOD PLEASE YES
PLEASE add this.Please say why you support.
- plqaokwsijeduhrfyg
- Scratcher
500+ posts
switch / case block
So it would be something like this:
Support. Really annoying when you have to add 40 if blocks.
Yes I used tynker for this don’t hurt me.
Support. Really annoying when you have to add 40 if blocks.
Yes I used tynker for this don’t hurt me.
Last edited by plqaokwsijeduhrfyg (April 27, 2021 03:58:46)
- k0d3rrr
- Scratcher
1000+ posts
switch / case block
I'll help bring this topic up to be helpful.
Honestly, I would still use the classic “if <> then, else” block for this, but this would help save blocks in projects.
However, I wouldn't want this to replace the “if <> then, else” block if this was implemented.
Honestly, I would still use the classic “if <> then, else” block for this, but this would help save blocks in projects.
However, I wouldn't want this to replace the “if <> then, else” block if this was implemented.