By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

how does ?: work

P: n/a
Tom
Hi

Is this a conditional ? what is the structure of the statement?
ch Tom
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Yep, it's a conditional.

test ? true_outcome : false_outcome;

So, say you want to add one if the value is less than 5, zero
otherwise:

value = value + (value < 5) ? 1 : 0;

Peace,
--Carl

Nov 16 '05 #2

P: n/a
Tom wrote:
Hi

Is this a conditional ? what is the structure of the statement?
ch Tom

string path = pathvar.trim() == string.empty ? defaultpath : pathvar;

if pathvar.trim returns string.empty then path will be set to
defaultpath, otherwise pathvar.

HTH
JB
Nov 16 '05 #3

P: n/a
Watch for word wrap on the link:

http://msdn.microsoft.com/library/de...aloperator.asp
Nov 16 '05 #4

P: n/a
hi
The conditional operator (?:) returns one of two values depending on the
value of a Boolean expression.

syntax is
cond-expr ? expr1 : expr2
cond-expr is An expression of type bool.
expr1 An expression.
expr2 An expression.
Remarks
If cond-expr is true, expr1 is evaluated and becomes the result;
if cond-expr is false, expr2 is evaluated and becomes the result.
Only one of expr1 and expr2 is ever evaluated.
regards
Ansil
Trivandrum
"Tom" wrote:
Hi

Is this a conditional ? what is the structure of the statement?
ch Tom

Nov 16 '05 #5

P: n/a
"Tom" <To**********@hotmail.com> wrote in news:ON**************@TK2MSFTNGP11.phx.gbl:
Hi

Is this a conditional ? what is the structure of the statement?


Tom,

http://msdn.microsoft.com/library/de...alOperator.asp

or

http://tinyurl.com/46weh

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #6

P: n/a
Hello
The + operator takes precedence over the ?: operator.
It shoulr be written
value = value + (value < 5? 1 : 0);

Best regards,
Sherif
<ca***********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Yep, it's a conditional.

test ? true_outcome : false_outcome;

So, say you want to add one if the value is less than 5, zero
otherwise:

value = value + (value < 5) ? 1 : 0;

Peace,
--Carl

Nov 16 '05 #7

P: n/a
"Tom" <To**********@hotmail.com> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
Hi

Is this a conditional ? what is the structure of the statement?


Lots of good answers already, but I'll add my 2 cents: ?: is nothing but
shorthand for a longer construction. A tediously common pattern in almost
any programming context is to have to write something of the form:

if(condition)
variable = value1;
else
variable = value2;

Kernighan and Ritchie, when they cooked up C many years ago, recognized that
the point of that code, its underlying semantics, is to assign to the
variable. But the assignment has become disguised by wrapping it in an if
statement. Recognizing that this is such a common pattern they cooked up
the ?: operator to allow developers to make the intention of their code more
explicit. Thus, instead of hiding the assignment inside of an if(), you
hide the if() inside of the assignment:

variable = condition ? value1 : value2;

Not only can this make your code (slightly) more self-documenting, if the
actual variable name in question is something long and complicated, the ?:
version can certainly be easier to type. Either way--written as an if()
block or as an assignment--both pieces of code should compile to exactly the
same machine code (or IL or whatever).

The drawback, of course, is that this solution is not very scalable to
situations where you have more than a boolean choice to make. People who
favor the use of ?: can be tempted to nest them, as in:

variable = condition1?value1:(condition2?value2:value3);

but this sort of thing quickly--no, immediately--becomes difficult to read,
understand, debug, and maintain. Personally, I don't use ?: very much
myself, and generally only in the context of parameters to function calls;
if I have a lengthy function call, something like:

function(paramter1, parameter2, paramater3,
"some long string",
parameter4, parameter5);

and then I find that in some circumstances parameter5 needs to have one or
the other value, then it's much easier to write:

function(paramter1, parameter2, paramater3,
"some long string",
parameter4,
condition?value1:value2);

than it is to replicate the entire function call inside an if(condition)
block. It's also easier to maintain if I end up changing the values of some
of the other parameters. At any rate, when used wisely ?: can make your
code shorter, easier to read, and easier to maintain. Used unwisely, it can
turn otherwise clean code into an obfuscated mess.
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.