Discuss Scratch
- Discussion Forums
- » Suggestions
- » ?-Operator [inline if-then-else]
- 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:
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
- 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:
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.
Scratch Team Member, kayak and pickleball enthusiast, cat caregiver.
This is my forum signature! On a forum post, it is okay for Scratchers to advertise in their forum signature. The signature is the stuff that shows up below the horizontal line on the post. It will show up on every post I make.
(credit to Za-Chary)
;
- PH-zero
- Scratcher
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)
- PH-zero
- Scratcher
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
- Scratcher
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
- scimonster
- Scratcher
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
Retired Community Moderator
BTW, i run Google Chrome 41.0.2272.101 on a Linux system - Ubuntu 14.04. NEW: iPad 4th gen. w/retina.
418 I'm a teapot (original - to be read by bored computer geeks)
THE GAME (you just lost)
; THE SEMICOLON LIVES ON IN OUR SIGS
- DevanWolf
- Scratcher
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
Online YouTube Poop Generator is Here! | SimTunes! | Bring Back Backdrop Scroll Blocks! | New Scratch Blocks! | Scratch Soundfont! | Important MIDI Converter!
Please check out those cool games that you will like! Click a game! Any game!
when(mouse y)>(150)Please help Scratch Team remove the 150px Signature Height Cap!
repeat until <(programmers::extension)>(150)>
delete this cap::control cap
end
delete this limit::control cap
- Mrcomputer1
- Scratcher
500+ posts
?-Operator [inline if-then-else]
+1
My Profile / My User Page / My Talk Page
——————–
Progress bar to 1000+ Posts - Image might not be up to date
——————–
My browser / operating system: Windows NT 10.0 (Windows 11 - 22H2), Firefox 122.0b4
——————–
My ScratchX Extensions——–If you like this post, give me an internet!——–Sharp Scratch Mod
- 20btheilmanngohr
- Scratcher
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
- Scratcher
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)>
$(".box-head")[0].textContent = "committing AT crimes since $whenever"
- PH-zero
- Scratcher
100+ posts
?-Operator [inline if-then-else]
Duplikate, I suggested the same a while ago:
http://scratch.mit.edu/discuss/topic/33995/?page=1#post-286812
And support of course
http://scratch.mit.edu/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 []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
when [Programming v] key pressedScience, PHP, C++, C, Java, VBS, Batch and JavaScript
- themusicmanooo
- Scratcher
69 posts
?-Operator [inline if-then-else]
well i kind of support but this is great
when [Programming v] key pressedScience, PHP, C++, C, Java, VBS, Batch and JavaScript
- stickfiregames
- Scratcher
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
- 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) 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)
The above post was likely clear sass. 10 outta 10.
Unless it wasn't. Which happens from time to time. Sadly.