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,
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

Chibi-Matoran wrote:

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.
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.
Sheep_tester
Scratcher
500+ posts

switch / case block

I believe that this isn't exactly the workaround for the switch statement:

FiveCubed wrote:

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
because if I did something such as
if <(color) = [blue]> then
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
all the if blocks would run because the variable “color” is being changed.

But yeah, support.
gdpr533f604550b2f20900645890
Scratcher
1000+ posts

switch / case block

Sheep_tester wrote:

I believe that this isn't exactly the workaround for the switch statement:
-snip-
because if I did something such as
if <(color) = [blue]> then
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
all the if blocks would run because the variable “color” is being changed.

But yeah, support.
FiveCubed's workaround is correct, because the switch construct needs a break statement or else it will continue to the next case.
public class test {
	public static void main(String[] args) {
		int n = 0;
		switch (n)
		{
		case 0:
			n = 1;
		case 1:
			System.out.println(n);
		}
	}
}
This code will print the number one.

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
sathvikrias
Scratcher
500+ posts

switch / case block

BUMP!
PGBFLITE6373
Scratcher
1000+ posts

switch / case block

sathvikrias wrote:

BUMP!
I'm startig to think your @topicbumper
fdreerf
Scratcher
1000+ posts

switch / case block

PGBFLITE6373 wrote:

sathvikrias wrote:

BUMP!
I'm startig to think your @topicbumper
You are thinking wrong then.

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

fdreerf wrote:

PGBFLITE6373 wrote:

sathvikrias wrote:

BUMP!
I'm startig to think your @topicbumper
You are thinking wrong then.
No i think its there alt also imma get back on topic
Thank god you closed your scratchblock tags
PkmnQ
Scratcher
1000+ posts

switch / case block

PGBFLITE6373 wrote:

fdreerf wrote:

PGBFLITE6373 wrote:

sathvikrias wrote:

BUMP!
I'm startig to think your @topicbumper
You are thinking wrong then.
No i think its there alt also imma get back on topic
Thank god you closed your scratchblock tags
It's literally fdreerf's alt.

Anyway, it would be weird, but if they manage to implement it sensibly somehow, then support.
PGBFLITE6373
Scratcher
1000+ posts

switch / case block

PkmnQ wrote:

PGBFLITE6373 wrote:

fdreerf wrote:

PGBFLITE6373 wrote:

sathvikrias wrote:

BUMP!
I'm startig to think your @topicbumper
You are thinking wrong then.
No i think its there alt also imma get back on topic
Thank god you closed your scratchblock tags
It's literally fdreerf's alt.

Anyway, it would be weird, but if they manage to implement it sensibly somehow, then support.
imma make an alt and reply
sathvikrias
Scratcher
500+ posts

switch / case block

PGBFLITE6373 wrote:

No i think its there alt
no i'm not topicbumper's alt .
this suggestion would need varadic inputs, aka @addInput and @delInput in scratchblocks.
PGBFLITE6373
Scratcher
1000+ posts

switch / case block

sathvikrias wrote:

PGBFLITE6373 wrote:

No i think its there alt
no i'm not topicbumper's alt .
this suggestion would need varadic inputs, aka @addInput and @delInput in scratchblocks.
yes i know that your not, but you seem like it. other then that support has less clutter
awesome_guy6856
Scratcher
100+ posts

switch / case block

OH MY GOD PLEASE YES
SatyrnWasTaken
Scratcher
1 post

switch / case block

PLEASE add this.
plqaokwsijeduhrfyg
Scratcher
500+ posts

switch / case block

awesome_guy6856 wrote:

OH MY GOD PLEASE YES

SatyrnWasTaken wrote:

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.

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.

Powered by DjangoBB