Click to See Complete Forum and Search --> : C++ Question ? (bCondition) ? TRUE : FALSE


Braulio
August 10th, 1999, 03:33 AM
Hi,

Can anybody exaplain me, what is this:

(bCondition) ? TRUE : FALSE

I always see it every where, but I don't know very well for what thing is this ( is better than an "If" command) ?

Thanks, Bye !
Braulio

Burlacu Ovidiu
August 10th, 1999, 03:46 AM
this is ?: operator and work in this way:

atype a, s, t;

a = (bool_cond) ? s:t;//if bool_cond ==true a= s, else a = t




Let me know if this help u
Regards,
Ovidiu

Braulio
August 10th, 1999, 03:49 AM
Thanks,

One question more, Is better than a normal "If" ?, Or is a good think to know to understand code from other people .... :-( ?

Thanks, Bye !
Braulio

August 10th, 1999, 04:20 AM
Its the tertiary or trinary operator. A specialized version of the more common if-else condition
u can refer to any book on C or even MSDN to get more information.

(bCondition) ? TRUE : FALSE

This means if the condition is TRUE then use the first value (TRUE) and if the condition returns FALSE then use the second value (FALSE)

instead of TRUE/FALSE one can also have other data types or calculations
General syntax is
test ? statement1 : statement2
this means
if (test)
statement1
else
statement2

Burlacu Ovidiu
August 10th, 1999, 04:30 AM
The ?: operator is a shortcut for an if...else statement. It is typically used as part of a larger expression where an if...else statement would be awkward. For example:

CString now;
int h;
//read h
var greeting = "Good" + (h != 0) ? " evening." : " day.");




The example creates a string containing "Good evening." if h != 0. The equivalent code using an if...else statement would look as follows:

CString now = "Good";
int h;
//read h
if (h!=0)
greeting += " evening.";
else
greeting += " day.";





Hope this help u
Regards,
Ovidiu