Ternary operator in calculation block?

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
Post Reply
MJU20
Posts: 244
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Flowcode v10 Ternary operator in calculation block?

Post by MJU20 »

Ternary operator in calculation block?
Wouldn't that be a hoot?

Code: Select all

val2 = (val1 < 20)?3:2
https://en.wikipedia.org/wiki/Ternary_c ... l_operator

kersing
Valued Contributor
Posts: 157
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 64 times
Been thanked: 58 times

Re: Ternary operator in calculation block?

Post by kersing »

Yep, that’s what I thought a couple of years ago as well. Got declined as being unreadable for novice users. Hopefully it will be reconsidered…

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Ternary operator in calculation block?

Post by mnfisher »

One trick I use a lot - allows an imitation of the ternary operator in a lot of cases.

Boolean operations return 1 for true and 0 for false.

So use:

.v1 = (.v2 < 20) + 2. // 3 if v2 is < 20 else 2

.flag = (.v == 10) // flag true if v = 10

Martin

kersing
Valued Contributor
Posts: 157
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 64 times
Been thanked: 58 times

Re: Ternary operator in calculation block?

Post by kersing »

Thanks for the hint, however strictly speaking false is zero and true might be any value not zero, so this working depends compiler/controller (and a bit of luck?)

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Ternary operator in calculation block?

Post by mnfisher »

True - but I've not had any issues so far. Also use if(.x) rather than if(.x !=0 ) - would be interesting to know if it actually saves any memory / or time. Must do a test...

It could come back to bite me someday :shock:

Martin

kersing
Valued Contributor
Posts: 157
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 64 times
Been thanked: 58 times

Re: Ternary operator in calculation block?

Post by kersing »

Any halfway decent compiler should optimize the expression, BTW, you probably don’t want to include a space between the exclamation mark and the equal sign as it should throw a syntax error. The correct syntax is if ( .x != 0 )
And in flowcode you can use <> which is easier on the eyes for novice programmers…

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Ternary operator in calculation block?

Post by mnfisher »

Autocorrect on my phone..

I read it as not(!) equal - nice that either works...

MJU20
Posts: 244
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Ternary operator in calculation block?

Post by MJU20 »

Thanks guys for this input.

The trick from mnfisher I will remember, together with the remark that "false is zero and true might be any value not zero"..

As a non programmer readability is necessary for me, so I mostly use <>.
Flowcode is a nice program to not need to read code so the simpeler it gets the better.
But for an example like val2 = (val1 < 20)?3:2 there is a lot of commands needed..

I will start a Onenote to keep these tricks for me to remember.

Post Reply