silly question about c++

Can somebody explain what the : and the ? symbols are doing in this line:

cout << (row % 2 ? “x” : “<”);

e.g lets say row = 5 and modulos 2 of 5 is 1, What are the functions of the above symbols.

Thanks…

It’s like an if-else:

TEST ? EXPRESSION_1 : EXPRESSION_2

is equivalent to EXPRESSION_1 if TEST is non-zero, EXPRESSION_2 otherwise.

Edit: Syntactically I believe it is a single expression.

Malcolm

hgallo wrote:

>
> Can somebody explain what the : and the ? symbols are doing in this
> line:
>
> cout << (row % 2 ? “x” : “<”);
>
> e.g lets say row = 5 and modulos 2 of 5 is 1, What are the functions of
> the above symbols.
>
> Thanks…
>
>

if row %2 then assign “x” else assign “<” and pass it to cout


openSUSE 11.2 64 bit | Intel Core2 Quad Q8300@2.50GHz | Gnome 2.28 | GeForce
9600 GT | 4GB Ram
openSUSE 11.3 64 bit | Intel Core2 Duo T9300@2.50GHz | Gnome 2.30 | Quadro
FX 3600M | 4GB Ram

Hi,

What the others have said seems to explain this well enough; I would
just like to add one thing: this is known as the ‘ternary operator’.
You can look this up for more information: any good C++ textbook should
cover it.


Regards,
Barry

Thank you so much to all, This really makes it clear. Thanks again.