How is this Nested Compound Ternary Statement Evaluted and Executed?

12 Views Asked by At

How is this compound ternary conditional expression interpreted and executed?

I would like to change this over to a nested if / else set of coding, which I think would be much easier to read and promote better understanding.

Many thanks, in advance.

x > nz(fit[1]) ? ((x - r) < nz(fit[1]) ? nz(fit[1]) : (x - r)) : ((x + r) > nz(fit[1]) ? nz(fit[1]) : (x + r))
1

There are 1 best solutions below

1
On

Here is a version translated using if and else:

var some_variable;

if (x > nz(fit[1])) {
    if ((x - r) < nz(fit[1])) {
        some_variable = nz(fit[1]);
    }
    else {
        some_variable = (x - r);
    }
}
else if ((x + r) > nz(fit[1])) {
    some_variable = nz(fit[1]);
}
else {
    some_variable = (x + r);
}

Note that the ternary expression you provided returns a value, and was most likely being used to make an assignment. So, I have added a variable some_variable which receives the value generated by the ternary expression.