[prog] Re: weird <? operator

Rachel McConnell rachel at xtreme.com
Sat May 31 04:44:19 EST 2003


I'll take a stab at this WRT Java - if there are C/C++ related
differences someone must correct me.  Yes it's an if-else statement, but
not _necessarily_ simple ;)

In Java and other languages, there is a thing called the ternary
operator, so named because it takes three arguments.  The syntax is

   arg1 ? arg2 : arg3

where arg1 is evaluated to a boolean, and only one of arg2 or arg3 is
evaluated, based on the result of arg1.  It's frequently used for
assignments:

   variableName = booleanTest ? expressionA : expressionB ;

where, if booleanTest evaluates to true, variableName is assigned to the
result of expressionA, if false, it's assigned to expressionB.

However the 'variableName = ' bit is not required, you could also do
something like this:

   varN > 100 ? varN-- : varN++ ;

similar to your example, although I've boldly switched from > to < in
mine.  Note that in Java (and probably elsewhere), the order of
operations requires the ternary operator to be evaluated prior to the
assignment operator, so if you wanted to test equality you'd need a set
of parentheses, like so:

   ( varX = 100 ) ? varX++ : varX-- ;

this is a horrible example of unclear code though; what I've just said
is, assign varX to 100; next, if (100) then increment varX, otherwise
decrement it.  I can't remember (and it's too late to test) what 100
will return if evaluated as a boolean - one good reason to avoid this
construction.  I actually can't think of any reason to use this kind of
confusing construction, but I'll leave the example as it does illustrate
order of operations.

I admit to some confusion over the original <? as that would seem to
imply at least a corresponding =? and either an <=? or >? (or both plus
>=? or how about !=? or <>?   yikes).  Someone did seem to know - do you
have further info on it?

Rachel

-----Original Message-----
From: programming-bounces at linuxchix.org
[mailto:programming-bounces at linuxchix.org] On Behalf Of Guru -
Sent: Friday, May 30, 2003 8:36 PM
To: programming at linuxchix.org
Subject: [prog] Re: Programming Digest, Vol 3, Issue 21


> > And that weird command, <?   I see that
> > it's short for
> > result = result < 100 ? 100 : result
> > which is a lot better way to do it
>
>i've never heard of that... where'd you hear about that?

Can you do it like that?
The command does exist, I've seen it in my C book (1st year CS student).
As far as I know wouldn't it be (a new example): result < 100 ? result++
: result--

That pointless piece of code I just created should, if the result is
less 
than 100 add 1 to result and if its greater than 100 it will take 1 away

from result.

I believe that the command is simply short for a very simple if-else 
statement.

Is that correct?

_________________________________________________________________
ninemsn Extra Storage is now available. 30MB of storage on ninemsn
Groups - 
great for sharing photos and documents. Go to  
http://join.msn.com/?page=dept/home&pgmarket=en-au

_______________________________________________
Programming mailing list
Programming at linuxchix.org
http://mailman.linuxchix.org/mailman/listinfo/programming



More information about the Programming mailing list