ternary operator

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
Blinky
Posts: 8
Joined: Mon Sep 03, 2018 12:23 am
Contact:

ternary operator

Post 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

kersing
Valued Contributor
Valued Contributor
Posts: 1939
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: ternary operator

Post by kersing »

Not available. I've been asking for it for ages but it is considered too confusing for novice programmers I think.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

mnf
Valued Contributor
Valued Contributor
Posts: 796
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: ternary operator

Post 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 85 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

kersing
Valued Contributor
Valued Contributor
Posts: 1939
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: ternary operator

Post 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.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Blinky
Posts: 8
Joined: Mon Sep 03, 2018 12:23 am
Contact:

Re: ternary operator

Post by Blinky »

Thank you guys. I'm grateful for your input! Will try that solution now.

Post Reply