Ternary (Conditional Values)

A feature I’d like to see in Hopscotch is:

ternary (conditional values)

I’d use it for this type of project:

every project, especially those that could benefit from this block if it was added

Here are some examples of Hopscotch code that could be improved if we had this:

it would be an alternate and simpler method to the check if else container. It would also simplify a lot of code in some cases, which is very useful for coding something like a digital clock in Hopscotch, which often needs a lot of check if else blocks to display the text as one would expect…

It would also allow some code to run faster in some cases, even instantly, rather than at least 1 frame per conditional.

Here is an example design I’ve made:

(Updated September 28, 2023)

Block Name/Description: “Conditional Value”
Type: 57
Category: To Be Determined…

Example Help Docs for the block:

Conditional Value

------------------

this will return one of two value based on the set condition.

this acts much like a conditional (ternary) operator.

examples:

(if 7=7 then "apples" else "bananas") - this will return "apples" since 7 is equal to 7

(if 5>3 then 5 else 3) - this will return 5 since 5 is greater than 3

(if 7<8 then 25 else 10) - this will return 10 since 7 is not less than 8

I’ve seen something similar in another programming languages, it looks like this:

JavaScript:

...=(test==1)?"yes":"no";
12 Likes

This could also make platformers possible again in the newer player version(s) without the need for the modded tools. It’s a win-win for everyone…

5 Likes

I love the Swift 5.9 release with the new ternary syntax.

Instead of the ugly:

let foo = Bool.random() ? “Yes” : “No”

You can now do:

let foo = if Bool.random() { “Yes” } else { “No” }

Which is easier to read

I know that wasn’t exactly hopscotch related, but I love ternary operators!

7 Likes

This would be cool! I like the Python syntax for it and feel like it would translate great to Hopscotch:

print("Apple " + "es" if apples != 1 else "e")
11 Likes

Those are some great examples. I guess the hardest part about this one is choosing the right order of parameters… so far @/William04GamerA’s example looks best for beginners, imo, although others might be more used to having the conditional part first…

3 Likes

I haven’t really used ternaries before but this’d be amazing

2 Likes

Alright, time to settle this:

which ternary function do you think is easiest to understand?
  • My JavaScript example (top post)
  • JonnyGamer’s SwiftUI example
  • William04GamerA’s python example
0 voters
3 Likes

i like william’s the most for readability, but i wouldn’t be opposed to the others! :grin:

I’d love to see a more refined system for managing calculations in a single block in general. Some of my favorite functions are made in a single block (such as generating a grid with a single set position & clones) and having a more readable system would make it much more enjoyable to make these optimizations!

3 Likes

I think William’s is the easiest to understand for beginners, but yours seems like it might be more convenient for more advanced users.
But that might just be my opinion.

2 Likes

Regardless of the syntax, all 3 examples will do the same thing…

2 Likes

I’ve actually came across this problem where you have to add a lot of unnecessary code, and it would be cool to add this :grin:

This would actually make coding a lot easier for a lot of people. I’ve actually given up on things bc of too many bugs with all the unnecessary code

(I’m probably stating something else but regardless I would also like to see this)
And I think yours is the easiest to understand (@/Crosbyman64)

1 Like

I just thought of a few things:

Example Block Description: “Ternary” (it’s short, simple, and to the point)

Example Help Docs description:

ternary conditionals have 3 arguments: a conditional and two expressions. 
The first expression is returned if the condition is true, otherwise, 
the other expression is returned. This could be used as an alternate method
to using the Check If Else block.

examples:

(1 if 7=7 else 0) will return 1 since 7=7 is true
(1 if 6=7 else 0) will return 0 since 6=7 is false
2 Likes

I was kind of interpreting it as a “check if” block, block.

That would make the ternary block a more of a math block of all things. (Or text variable as represented in the first post) but otherwise that’s a really reasonable way to describe it

Maybe it could be a way of shrinking code into smaller things, not needing the “check if” block. But that block would still have use because of other related issues in code. So overall this is an idea that I would seriously consider if I where a hopscotch app moderator

1 Like

Well, the actual ternary conditional is frequently used as an alternative to the if…else statement in JavaScript. Even says so in its description.

JS ternary

2 Likes

Oh ok. Well, that’s sad. I can’t do JavaScript (I don’t have the access but I understand it)

1 Like

Why? Is it blocked?

The Shortcuts App can do JavaScript, but you would need to be able to turn on Allow running of scripts under advanced…

2 Likes

Yeah it’s blocked. Sadly

1 Like

Also, JavaScript has been on…

2 Likes

Bummer… and that would probably also explain why catbox doesn’t work for you…

e: thinking about it, it doesn’t make any sense since JavaScript is only required for the drag and drop function in catbox, but the entire site itself is blocked on your end…

2 Likes

Let’s say my school is overprotective

1 Like