ternary operator

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

Moderator: Benj

Blinky
Posts: 8
Joined: Mon Sep 03, 2018 12:23 am
Has thanked: 9 times
Contact:

ternary operator

Postby Blinky » Mon Nov 19, 2018 11:21 pm

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: 1878
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 535 times
Been thanked: 1043 times
Contact:

Re: ternary operator

Postby kersing » Tue Nov 20, 2018 12:21 am

Not available. I've been asking for it for ages but it is considered too confusing for novice programmers I think.
These users thanked the author kersing for the post:
Blinky (Thu Nov 22, 2018 5:09 pm)
Rating: 5.26%
 
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

mnf
Valued Contributor
Valued Contributor
Posts: 594
Joined: Wed May 31, 2017 11:57 am
Has thanked: 56 times
Been thanked: 331 times
Contact:

Re: ternary operator

Postby mnf » Tue Nov 20, 2018 9:09 am

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
Capture.PNG (5.79 KiB) Viewed 1567 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 61 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
These users thanked the author mnf for the post:
Blinky (Thu Nov 22, 2018 5:09 pm)
Rating: 5.26%
 

kersing
Valued Contributor
Valued Contributor
Posts: 1878
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 535 times
Been thanked: 1043 times
Contact:

Re: ternary operator

Postby kersing » Tue Nov 20, 2018 9:58 am

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.
These users thanked the author kersing for the post:
Blinky (Thu Nov 22, 2018 5:09 pm)
Rating: 5.26%
 
“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
Has thanked: 9 times
Contact:

Re: ternary operator

Postby Blinky » Thu Nov 22, 2018 5:09 pm

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