unusual language construct

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

pos-- the result of State ==0 or ==1?

Thanks

Google "C ternary operator"

google knows. even if you search for ?:

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.

rynd2it:
This piece of code was written by someone else and I have never seen it used before.
(State ? pos-- : pos++))

You probably will never see such an application of the ?: operator again.

The ?: construct selects a value by a condition,
in your case the selected value is the same in both cases, which is rather pointless.

IMHO a simple if to select between decrement and increment of pos would be easier to read.

Juraj:
google knows. even if you search for ?:

About 116,000,000 results (0.50 seconds)

none of them helpful

rynd2it:
About 116,000,000 results (0.50 seconds)

none of them helpful

sorry. google knows too much and gives me a different result

TheMemberFormerlyKnownAsAWOL:
Google "C ternary operator"

All right - after 40+ years programming etc I Learned a new term :slight_smile:

rynd2it:
About 116,000,000 results (0.50 seconds)

none of them helpful

google ternary operator
They are helpful; you might not understand them but that's another matter.

From one of the links found (Ternary Operator in C Explained)

Programmers use the ternary operator for decision making in place of longer if and else conditional statements.

thapthoptheep:
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.

Thanks for the detailed and informative reply, much appreciated

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.