Discuss Scratch
- Discussion Forums
- » Suggestions
- » ?-Operator [inline if-then-else]
- PH-zero
-
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:
A possible use:
istead of
Any thoughts?

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)value would be set to 5 because 1=/=2
set [value v] to (if <[1] = [2]> then [true!] else [false...] ::operators)
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
-
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:
Perhaps even more easily understood than the in-line version.
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
-
100+ posts
?-Operator [inline if-then-else]
in-line if-else, wow that describes it very well 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.

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
-
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
-
100+ posts
?-Operator [inline if-then-else]
Yeah I also found that really cool 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>>))

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
-
58 posts
?-Operator [inline if-then-else]
Mabye like this.Yeah I also found that really cool 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>>))
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
if <...> then [1] else [2] :: operators reporter
- spgame05
-
100+ posts
?-Operator [inline if-then-else]
Support! 

if <> is true, return [] else return [] :: control reporter
- scimonster
-
1000+ posts
?-Operator [inline if-then-else]
Long suggested, and i've always supported, specifically with this wording.Mabye like this.Yeah I also found that really cool 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>>))
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=operatorsif <...> then [1] else [2] :: operators reporter
- DevanWolf
-
100+ posts
?-Operator [inline if-then-else]
We need this block:
If the boolean in the block reports true, it reports the first value. Otherwise, if false, it reports the else value.
For an example:
(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
- 20btheilmanngohr
-
100+ posts
?-Operator [inline if-then-else]
So wait, what's the problem with
when green flag clickedYou 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
ask [Are you cool?] and wait
if <> then
say [Good!] for (2) secs
else
say [What!?] for (2) secs
end
- MegaApuTurkUltra
-
1000+ posts
?-Operator [inline if-then-else]
Regular programming (most languages) has the lazy if-else statement:
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
derp = (five > 5) ? 5 : 6;
// set derp to 5 if five > 5, or 6 otherwise
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?>so I don't see why it can't have this as well.
// same as
<(loudness) > (30)>
- PH-zero
-
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
http://scratch.mit.edu.ezproxyberklee.flo.org/discuss/topic/33995/?page=1#post-286812
And support of course

- themusicmanooo
-
69 posts
?-Operator [inline if-then-else]
you could make a block of it in more blocks
if <> then [] else []and wow that teached me how to make if then else in more blocks
define if (boolean) then (string) else (string)
if <(boolean) = [true]> then
say [good]
else
say [WHAT!]
end
- themusicmanooo
-
69 posts
?-Operator [inline if-then-else]
well i kind of support but this is great

- stickfiregames
-
1000+ posts
?-Operator [inline if-then-else]
you could make a block of it in more blocksif <> then [] else []and wow that teached me how to make if then else in more blocks
define if (boolean) then (string) else (string)
if <(boolean) = [true]> then
say [good]
else
say [WHAT!]
end
This would be so much more useful if we had custom reporters…
- little_kitten
-
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) secsNow 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)
- Discussion Forums
- » Suggestions
-
» ?-Operator [inline if-then-else]