Conditional/ternary Statement

Forrest Gehrke,2C-119,7239,ATTBL feg at clyde.ATT.COM
Thu Feb 8 05:38:27 AEST 1990


OK, sorry people.  I will get my example statement
right, perhaps on the third attempt (:-<)

Maybe I have answered my own question: the thing is
too messy to write!!

Now suppose one were sorting among unsigned ints from
0 to 4.  Would this be kosher?

#define Srt(x) ((x)==0 ? 0 : (x) < 2 ? 1 : (x) < 3 ? 2 : (x) < 4 ? 3 : 4)

It would seem to be valid, but only if exited immediately
at the first true test.  If kosher, are there any speed 
advantages over a series of  if-else, or case statements?

(I realize that a pitfall might occur if the value range
of the tested variables were less than zero or more
than 4--this is just a conjured example and is really
a question whether multiple ternary operators may be 
used in a single statement).

Forrest Gehrke feg at clyde.ATT.COM



More information about the Comp.lang.c mailing list