Page 1 of 1

ternary operator

Posted: Mon Nov 19, 2018 11:21 pm
by Blinky
Hi all

was just wondering if the ternary operator is available in FlowCode? More generally shown as 'condition ? caseA : caseB'
I've tried a few things but to no avail.

Many thanks

Re: ternary operator

Posted: Tue Nov 20, 2018 12:21 am
by kersing
Not available. I've been asking for it for ages but it is considered too confusing for novice programmers I think.

Re: ternary operator

Posted: Tue Nov 20, 2018 9:09 am
by mnf
Although it would be nice - it's really redundant as it can easily be replicated with a conditional macro.

If you really want to use though - you can, just use a C macro. For example:
Capture.PNG
(5.79 KiB) Downloaded 1831 times
Remember that - the variable names are changed by FC to FCV_X or FCL_X where X is the FC variable name (in uppercase) - you can also call macros in the ternary operator - the easiest way to get the macro names is to take a peek using the 'view c' option.

Or as a complete (but very simple) example - that sets the value of variables 'b' and 'c' to 10 if a = 1 otherwise 0 - once using a conditional and once using the ternary operator...
Flowcode1.fcfx
(9.77 KiB) Downloaded 225 times
But I agree - a conditional block often looks clumsy for such simple assignments.

I wouldn't recommend it - but if you have a real dislike of branches in your code:

In a calculation:

Code: Select all

c = (a == 1) * 10
Relying on Boolean true equalling 1. This if you like 'write once' (ie unmaintainable & unreadable) code that may break in the future - I haven't tested it but it should work in simulation (and the C block may not?)

Martin

Re: ternary operator

Posted: Tue Nov 20, 2018 9:58 am
by kersing
mnf wrote:Although it would be nice - it's really redundant as it can easily be replicated with a conditional macro.
It would take a lot less screen estate and allow conditional updates within a calculation icon. Using C code does not help the readability of the Flowchart.

Re: ternary operator

Posted: Thu Nov 22, 2018 5:09 pm
by Blinky
Thank you guys. I'm grateful for your input! Will try that solution now.