Discuss Scratch

PH-zero
Scratcher
100+ posts

?-Operator [inline if-then-else]

Hey

Java has a very usefull operator,
it look like this:

int value = (playerAlive?1:0);

in Scratch it would probably look like this:

(if <> then [] else [] ::operators) 
set [value v] to (if <[1] = [2]> then [true!] else [false...] ::operators)
value would be set to 5 because 1=/=2

A possible use:
istead of
set [value v] to (((pick random (0) to (1))-(0.5))*(2))
to get 1 or -1 you can just use
set [value v] to (if <(pick random (0) to (1))=(1)> then [1] else [-1] ::operators) //I also think that this is way more legible

Any thoughts?

Last edited by Paddle2See (Feb. 22, 2021 11:16:10)

Paddle2See
Scratch Team
1000+ posts

?-Operator [inline if-then-else]

Basically, an in-line if-then-else construct. Handy, certainly, but nothing that can't be done fairly well with other blocks. For instance, on your last example, you could do the same thing with this:

if <(pick random (1) to (2)) = [1]> then
set [value v] to [1]
else
set [value v] to [-1]
end

Perhaps even more easily understood than the in-line version.
PH-zero
Scratcher
100+ posts

?-Operator [inline if-then-else]

Paddle2See wrote:

Basically, an in-line if-then-else construct. Handy, certainly, but nothing that can't be done fairly well with other blocks. For instance, on your last example, you could do the same thing with this:

if <(pick random (1) to (2)) = [1]> then
set [value v] to [1]
else
set [value v] to [-1]
end

Perhaps even more easily understood than the in-line version.
in-line if-else, wow that describes it very well
Of course you're right the only benefit seems to be that it would be “nice to have”.
But that example is pretty simple, imagine you have a huge algorythm and now you have to break it up into
an if-else. There are now 2 huge algorythms with only one little difference in them.

Last edited by PH-zero (March 30, 2014 18:08:22)

ProdigyZeta7
Scratcher
1000+ posts

?-Operator [inline if-then-else]

A little trick I learned with the booleans is that you can turn them into numbers:
define <condition> is true? yes: (value1) no: (value2)
set [value v] to (((value1) * <condition>) + ((value2) * <not<condition>>))
PH-zero
Scratcher
100+ posts

?-Operator [inline if-then-else]

ProdigyZeta7 wrote:

A little trick I learned with the booleans is that you can turn them into numbers:
define <condition> is true? yes: (value1) no: (value2)
set [value v] to (((value1) * <condition>) + ((value2) * <not<condition>>))
Yeah I also found that really cool
But you can't use this solution “in-line”
aaaaaaaand you can't multiply strings with 0 or 1 because that always gives you 0

Last edited by PH-zero (Sept. 9, 2014 20:12:15)

rolledpie41
Scratcher
58 posts

?-Operator [inline if-then-else]

PH-zero wrote:

ProdigyZeta7 wrote:

A little trick I learned with the booleans is that you can turn them into numbers:
define <condition> is true? yes: (value1) no: (value2)
set [value v] to (((value1) * <condition>) + ((value2) * <not<condition>>))
Yeah I also found that really cool
But you can't use this solution “in-line”
aaaaaaaand you can't multiply strings with 0 or 1 because that always gives you 0
(<>true? yes:[] no: []) //category=operators
Mabye like this.
if <...> then [1] else [2] :: operators reporter
rolledpie41
Scratcher
58 posts

?-Operator [inline if-then-else]

BuMp
spgame05
Scratcher
100+ posts

?-Operator [inline if-then-else]

Support!
if <> is true, return [] else return [] :: control reporter
PH-zero
Scratcher
100+ posts

?-Operator [inline if-then-else]

bumb
scimonster
Scratcher
1000+ posts

?-Operator [inline if-then-else]

rolledpie41 wrote:

PH-zero wrote:

ProdigyZeta7 wrote:

A little trick I learned with the booleans is that you can turn them into numbers:
define <condition> is true? yes: (value1) no: (value2)
set [value v] to (((value1) * <condition>) + ((value2) * <not<condition>>))
Yeah I also found that really cool
But you can't use this solution “in-line”
aaaaaaaand you can't multiply strings with 0 or 1 because that always gives you 0
(<>true? yes:[] no: []) //category=operators
Mabye like this.
if <...> then [1] else [2] :: operators reporter
Long suggested, and i've always supported, specifically with this wording.
DevanWolf
Scratcher
100+ posts

?-Operator [inline if-then-else]

We need this block:
(if<>then[]else[]::operators)
Like they have in some Scratch mods.

If the boolean in the block reports true, it reports the first value. Otherwise, if false, it reports the else value.

For an example:
when gf clicked
ask[Are you cool?]and wait
say(if<(answer)=[yes]>then[Good!]else[What!?]::operators)for(2)secs
Mrcomputer1
Scratcher
500+ posts

?-Operator [inline if-then-else]

+1
20btheilmanngohr
Scratcher
100+ posts

?-Operator [inline if-then-else]

So wait, what's the problem with
when green flag clicked
ask [Are you cool?] and wait
if <> then
say [Good!] for (2) secs
else
say [What!?] for (2) secs
end
You didn't say anything about how the boolean could be useful, you just said how it works and what it does. Unless there are some pros or cons listed, I think I'll stick to -1
MegaApuTurkUltra
Scratcher
1000+ posts

?-Operator [inline if-then-else]

Regular programming (most languages) has the lazy if-else statement:
derp = (five > 5) ? 5 : 6;
// set derp to 5 if five > 5, or 6 otherwise
It's great for lazy people like me who don't like typing more than necessary.

I think Scratch should have this as well because dragging blocks is annoying. I mean why do you think I made ApuC?
So I support. I mean Scratch already has lazy things like
<loud?>
// same as
<(loudness) > (30)>
so I don't see why it can't have this as well.
stickfiregames
Scratcher
1000+ posts

?-Operator [inline if-then-else]

Support.
PH-zero
Scratcher
100+ posts

?-Operator [inline if-then-else]

Duplikate, I suggested the same a while ago:
http://scratch.mit.edu.ezproxyberklee.flo.org/discuss/topic/33995/?page=1#post-286812

And support of course
themusicmanooo
Scratcher
69 posts

?-Operator [inline if-then-else]

you could make a block of it in more blocks
if <> then [] else []
define if (boolean) then (string) else (string)
if <(boolean) = [true]> then
say [good]
else
say [WHAT!]
end
and wow that teached me how to make if then else in more blocks
themusicmanooo
Scratcher
69 posts

?-Operator [inline if-then-else]

well i kind of support but this is great
stickfiregames
Scratcher
1000+ posts

?-Operator [inline if-then-else]

themusicmanooo wrote:

you could make a block of it in more blocks
if <> then [] else []
define if (boolean) then (string) else (string)
if <(boolean) = [true]> then
say [good]
else
say [WHAT!]
end
and wow that teached me how to make if then else in more blocks

This would be so much more useful if we had custom reporters…
little_kitten
Scratcher
500+ posts

?-Operator [inline if-then-else]

So, I saw a YouTube video on how to hack scratch and get this block:
(if<>then[]else[]) 
which basically, if the Boolean input was true, the string would be used, if it was false, though, the second would be used. It could be used like
say (if<key [space v] pressed?> then [Key space is pressed!] else [Key space is not pressed!] )for (2) secs
Now I thought, Well, wouldn't it be easier to just have it, and not have to hack and get it? And that's true. It would be easier.

Last edited by Paddle2See (Nov. 12, 2015 12:57:30)

Powered by DjangoBB