Discuss Scratch

red_king_cyclops
Scratcher
500+ posts

Add the conditional operator, a condensed if/else

In C, there is an operator known as the conditional operator ( ?: ). The conditional operator is pretty much a faster way to use a basic if/else. Since conditional operators are not that hard to learn and are really useful, I think Scratch 3.0 should have conditional operators.

The suggestion:

There are three conditional operators– the reporter type, the stack type, and the boolean type. All of them are in the control section. Each type is explained below.

Reporter type
(If <>, then [], else []::control)
The reporter type reports the value of the second input if the first is true or the value of the third input if the first is false.
Example:
when green flag clicked
set [applesEaten v] to [1]
say (join [I ate ] (join (applesEaten)(join [ apple] (If <[applesEaten] > [1]>, then [s.], else [.]::control)))
wait (2) secs
set [applesEaten v] to [2]
say (join [I ate ] (join (applesEaten)(join [ apple] (If <[applesEaten] > [1]>, then [s.], else [.]::control)))
Output of the example:
“I ate 1 apple.”
2 seconds later…
“I ate 2 apples.”

Stack type
If <>, then ([]::stack), else ([]::stack)::control
Note: the string inputs are placeholders for a new type of input called stack inputs.
The stack type executes the second input if the first is true or the third input if the first is false.
Example:
when green flag clicked
ask [Do you know Python? Y/N] and wait
If <(answer) = [Y]>, then (say [I know Python, too!] ::stack), else (say [I shall teach you Python!] ::stack)::control
Example output of the example:
“Do you know Python? Y/N”
>>> Y
“I know Python, too!”

Boolean type
<If <>, then <>, else <>::control>
The boolean type reports the value of the second input if the first is true or the value of the third input if the first is false.

Example:
when green flag clicked
say [Press either 1 or 2.]
set [key v] to [1]
wait until <If <(key) = [1]>, then <key [1 v] pressed?>, else <key [2 v] pressed?>::control>
say [You pressed the right key!]
Example output of the example:
“Press either 1 or 2”
*key 1 pressed*
“You pressed the right key!”

Notes:
  1. At the moment this section is unused.

Last edited by red_king_cyclops (Aug. 25, 2018 18:26:09)


2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
FutureBlack16
Scratcher
76 posts

Add the conditional operator, a condensed if/else

I think that might allow new possibilities, isn't it?

I like this idea.

ᖴᑌ丅ᑌᖇᗴᗷᒪᗩᑕᛕ

I try to be as respectful & helpful in Scratch forum as possible, i avoid using “Support” or “No Support” when i'm posting on suggestion forum, and i'm trying to make my posts as new scratcher friendly as possible by linking to an ITopic. I take care of necroposting, thread hijacks, etc. If you have any suggestions how to improve my posts and make my posts more helpful, be sure to click “Criticize my post” and leave a comment suggestion down below. Your feedbacks will help me to be more helpful in Scratch forum.
Community Guidelines - New scratcher? Tips here. - What is Scratch? - Criticize my posts
PrincessFlowerTV
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

No support.
What is wrong with
if <> then 



else

end
?

Got to be good looking cause he's so hard to see -The Beatles

(Thank you @just-there from The Profile Picture Shop for the banner ^^)





Nothing's down here, sorry to disappoint you.






JK. there may be something down there…









Maybe not, tho.











You're determined, aren't you?













Talar du Svenska? Om inte, det ar fin.





















Just stop it, will ya?

































Fine, you win.




Take a rice cake. *yeets you a rice cake*
Say “Thanks for that rice cake, Kewpie!” if you made it this far.
red_king_cyclops
Scratcher
500+ posts

Add the conditional operator, a condensed if/else

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
There's nothing wrong with the normal if/else, but the conditional operator can save time, go where a normal if/else can't, and can do basic if/elses faster, and isn't very confusing. The normal if/else has the advantages of being a little more readable and being able to do more complex tasks, so its not like the normal if/else is getting replaced.

Last edited by red_king_cyclops (July 31, 2018 21:21:10)


2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
PrincessFlowerTV
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

red_king_cyclops wrote:

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
There's nothing wrong with the normal if/else, but the conditional operator can save time, go where a normal if/else can't, and can do basic if/elses faster, and isn't very confusing. The normal if/else has the advantages of being a little more readable and being able to do more complex tasks, so its not like the normal if/else is getting replaced.
Still, I really see no reason to add the requested block; it doesn't do a whole lot of different things then the current block does.

Got to be good looking cause he's so hard to see -The Beatles

(Thank you @just-there from The Profile Picture Shop for the banner ^^)





Nothing's down here, sorry to disappoint you.






JK. there may be something down there…









Maybe not, tho.











You're determined, aren't you?













Talar du Svenska? Om inte, det ar fin.





















Just stop it, will ya?

































Fine, you win.




Take a rice cake. *yeets you a rice cake*
Say “Thanks for that rice cake, Kewpie!” if you made it this far.
red_king_cyclops
Scratcher
500+ posts

Add the conditional operator, a condensed if/else

PrincessFlowerTV wrote:

red_king_cyclops wrote:

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
There's nothing wrong with the normal if/else, but the conditional operator can save time, go where a normal if/else can't, and can do basic if/elses faster, and isn't very confusing. The normal if/else has the advantages of being a little more readable and being able to do more complex tasks, so its not like the normal if/else is getting replaced.
Still, I really see no reason to add the requested block; it doesn't do a whole lot of different things then the current block does.
True, but the conditional operator can be more convenient in some places.

2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
PrincessFlowerTV
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

red_king_cyclops wrote:

PrincessFlowerTV wrote:

red_king_cyclops wrote:

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
There's nothing wrong with the normal if/else, but the conditional operator can save time, go where a normal if/else can't, and can do basic if/elses faster, and isn't very confusing. The normal if/else has the advantages of being a little more readable and being able to do more complex tasks, so its not like the normal if/else is getting replaced.
Still, I really see no reason to add the requested block; it doesn't do a whole lot of different things then the current block does.
True, but the conditional operator can be more convenient in some places.
Still, no support. I can't think of any situations that the suggested block would be more conveniont.

Got to be good looking cause he's so hard to see -The Beatles

(Thank you @just-there from The Profile Picture Shop for the banner ^^)





Nothing's down here, sorry to disappoint you.






JK. there may be something down there…









Maybe not, tho.











You're determined, aren't you?













Talar du Svenska? Om inte, det ar fin.





















Just stop it, will ya?

































Fine, you win.




Take a rice cake. *yeets you a rice cake*
Say “Thanks for that rice cake, Kewpie!” if you made it this far.
red_king_cyclops
Scratcher
500+ posts

Add the conditional operator, a condensed if/else

PrincessFlowerTV wrote:

red_king_cyclops wrote:

PrincessFlowerTV wrote:

red_king_cyclops wrote:

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
There's nothing wrong with the normal if/else, but the conditional operator can save time, go where a normal if/else can't, and can do basic if/elses faster, and isn't very confusing. The normal if/else has the advantages of being a little more readable and being able to do more complex tasks, so its not like the normal if/else is getting replaced.
Still, I really see no reason to add the requested block; it doesn't do a whole lot of different things then the current block does.
True, but the conditional operator can be more convenient in some places.
Still, no support. I can't think of any situations that the suggested block would be more conveniont.
True, there isn't much reason to add a feature that nobody can think of a clever way to use. But what about the first example I used in the original post?

2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
Sheep_maker
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
The ternary operator exists in a lot of other programming languages; it also can make your code more concise and easier to read, which is very important in programming.

- Sheep_maker This is a kumquat-free signature. :P
This is my signature. It appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
TheAdriCoolManDude
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

Just so much duplicate.


red_king_cyclops
Scratcher
500+ posts

Add the conditional operator, a condensed if/else

bump #1

2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
ShinigamiBlacky
Scratcher
100+ posts

Add the conditional operator, a condensed if/else

red_king_cyclops wrote:

< if <>, then [] else [] :: control>
A better way to do that would just be to add custom reporters (Sratch Team, please)
If they dont come, Support

red_king_cyclops wrote:

If <>, then ((::stack control) :: grey ring), else ((::stack control) :: grey ring)::control
WHAT?? No Support, thats litterly just
if <> then 
else
end

red_king_cyclops wrote:

<if <>, then <> else<> :: control>
too easy of a workaround without much usefulness, No Support
(Also, its kinda redundant with the reporter thingy)
<<<if> and <Input then>> or <<not <if>> and <Input else>>>

Last edited by ShinigamiBlacky (Aug. 25, 2018 19:17:42)

Fupicat
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

I support the if <> then , else. I think there should also be a if <> then block, for when you don't need the else statement. It actually makes the code cleaner. The other blocks are just kind of redundant.



badatprogrammingibe
Scratcher
500+ posts

Add the conditional operator, a condensed if/else

I only support the suggestion for the reporter.

The boolean can be easily worked around:
<<<condition :: grey> and <first input :: grey>> or <<not <condition :: grey>> and <second input :: grey>>
_nix
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

badatprogrammingibe wrote:

The boolean can be easily worked around:
<<<condition :: grey> and <first input :: grey>> or <<not <condition :: grey>> and <second input :: grey>>
Problem is, you need to specify the condition twice there. That always looks bad (duplicated code is not a good thing here!), and if your condition is really, really long, it's very inconvenient too.

Last edited by _nix (Aug. 26, 2018 03:35:49)


══ trans autistic lesbian enbydoggirls // 16 17 18 19 20, she/they
sparrows one word to the paragraph // <3 // ~(quasar) nebula
Sheep_maker
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

Duplicate

- Sheep_maker This is a kumquat-free signature. :P
This is my signature. It appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
M4-
Scratcher
36 posts

Add the conditional operator, a condensed if/else

Support. This could be so useful for 1LCs.
Queer_Royalty
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

M4- wrote:

Support. This could be so useful for 1LCs.
As Sheep_Maker has pointed out, this is a duplicate. Please continue the discussion there.

Last edited by Queer_Royalty (June 25, 2021 12:31:52)


i would like to sincerely apologize for the above post.



i am stupid, weird, worthless, and in general a bad person but at least i am decent at platformers.
M4-
Scratcher
36 posts

Add the conditional operator, a condensed if/else

PrincessFlowerTV wrote:

red_king_cyclops wrote:

PrincessFlowerTV wrote:

red_king_cyclops wrote:

PrincessFlowerTV wrote:

No support.
What is wrong with
if <> then 



else

end
?
There's nothing wrong with the normal if/else, but the conditional operator can save time, go where a normal if/else can't, and can do basic if/elses faster, and isn't very confusing. The normal if/else has the advantages of being a little more readable and being able to do more complex tasks, so its not like the normal if/else is getting replaced.
Still, I really see no reason to add the requested block; it doesn't do a whole lot of different things then the current block does.
True, but the conditional operator can be more convenient in some places.
Still, no support. I can't think of any situations that the suggested block would be more conveniont.

I can think of many, for example a 1LC. Or what if we have a project that requires lots of if then else loops? Why not compact it down with this block?
DarthVader4Life
Scratcher
1000+ posts

Add the conditional operator, a condensed if/else

Queer_Royalty wrote:

M4- wrote:

Support. This could be so useful for 1LCs.
As Sheep_Maker has pointed out, this is a duplicate. Please continue the discussion there.
It's not there, unfortunately.

Be Moist Also, here's a helpful link to Ocular
I am DV4L, Erector of Text Walls, Typer of Long Posts, Creator of Mini-mod posts
The kumquats have eaten this line, so I have to have a new way to repel them.
Had a bad day or are feeling bored? Watch this to make your day better in an interesting way.
Do you think you've been banned unfairly? Then you'd be wrong. The ST aren't children, so quit acting like they are.

Powered by DjangoBB