Please someone correct me if I'm wrong but I understand ? and : to be
like this:
if (a<b) {};
Long way.
(a<b) ?/*dothis*/ : /*do that*/
Is this what the ? and : means?
No. The ternary operator isn't a statement selection entity, it is a /value/
selection entity. You can't seed the ternary operator's "true"
and/or "false" branches with statements that do not result in an value.
If not can someone so me a simple example?
The statement
a = (b < 0 ? 10 : 20);
is equivalent to
if (b < 0)
a = 10;
else
a = 20;
But, the statement
b < 0 ? return 4: while (1) {putchar(' ');};
is a syntax error (neither "return 4" nor "while (1) {}" are values).
The ternary operator (as it is called) consists of three parts:
1) a condition,
2) a value to be used when the condition is true, and
3) a value to be used when the condition is false.
The operator resolves to a single value, one of the two values given in it's
body. /Which/ value depends on the results of the condition.
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------