Regarding conditional operator '?' , can some one explain this to me, in simple if else format? AutoFFSuccess, ActSuccess, FUPSuccess are bool values.
char StatusCode = (AutoFFSuccess) ?
(ActSuccess) ?
'P' : 'W' : (FUPSuccess) ?
(ActSuccess) ?
'F' : 'G' : 'E';
I would normally format that like this:
char statusCode = AutoFFSuccess ? ActSuccess ? 'P' : 'W'
: FUPSuccess ? ActSuccess ? 'F' : 'G'
: 'E'
Or:
char statusCode;
if (AutoFFSuccess)
{
if (ActSuccess)
{
statusCode = 'P';
}
else
{
statusCode = 'W';
}
}
else
{
if (FUPSuccess)
{
if (ActSuccess)
{
statusCode = 'F';
}
else
{
statusCode = 'G';
}
}
else
{
statusCode = 'E';
}
}
It's really not very nice though.
Rather than express it with conditionals nested in that way, I'd separate each option out, using "stacked" single conditional operators. Note that some people don't like this, but I find it rather simple, once you're used to it:
char statusCode =
AutoFFSuccess && ActSuccess ? 'P'
: AutoFFSuccess ? 'W'
: FUPSuccess && ActSuccess ? 'F'
: FUPSuccess ? 'G'
: 'E';
Or to be more explicit:
char statusCode =
AutoFFSuccess && ActSuccess ? 'P'
: AutoFFSuccess && !ActSuccess? 'W'
: !AutoFFSuccess && FUPSuccess && ActSuccess ? 'F'
: !AutoFFSuccess && FUPSuccess && !ActSuccess ? 'G'
: 'E';
(I'm assuming the conditions won't change between evaluations, mind you...)
See more on this question at Stackoverflow