I apologise if this is a dumb question but I can't find the answer in any of the manuals or sample code.
This piece of code was written by someone else and I have never seen it used before. Please can someone point me to a reference that describes how this works:
(State ? pos-- : pos++))
The variable State is described as boolean but I'm not sure which value relates to which of the results
OP - what you have there is essentially a shorthand if condition - that's probably the best way to look at it.
Since 'State' will be either 0 or non-zero (false or true, respectively) this statements is saying, essentially:
If this is true ? do this : if it's not true then do this, instead
So, say I have an integer called 'num' and I want to add 1 to it if it's 0 or subtract 1 from it if it's > 0, I could do:
// Remember if num is 0 it's basically false, so the statement evaluates as false
num ? num-- : num++;
So if num >= 1 then this statement is true, since it's non-zero. We can also do this in if statements and loops if we want:
// This loop will run forever, since 1 is a numeric constant that is always true
while (1) {
...
}
// This code will never run, since 0 is false
int num = 0;
if (num) {
...
}
In a similar way we could do something like:
int num = 0;
true ? num++ : num--;
And since true is always definitely true you can be sure that num will be equal to 1 following the execution of this code.