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
ternary operator
Moderator: Benj
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
- Contact:
Re: ternary operator
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
― C.S. Lewis
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: ternary operator
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:
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...
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:
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
If you really want to use though - you can, just use a C macro. For example:
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...
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
Martin
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
- Contact:
Re: ternary operator
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.mnf wrote:Although it would be nice - it's really redundant as it can easily be replicated with a conditional macro.
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis