what does this statement mean?

J

Jay Wolfe

Hi,
I came across this statement and it has a confusing element. I know the
whole '?:" construct and the assignment within the compare, but what's up
with the comma?

All the variables here are declared as doubles.

rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2)

Specifically, (dx=x2-x1,x1)? How do I read this?

Thanks!

Jay
 
P

Phil Staite

Comma can be used to separate expressions, the result of the overall
expression is the result of the last one. So...

If f is less than zero, then dx gets set to x2 minus x1, and then the
value of x1 is assigned to rtb.
 
R

Rolf Magnus

Jay said:
Hi,
I came across this statement and it has a confusing element. I know the
whole '?:" construct and the assignment within the compare, but what's up
with the comma?

All the variables here are declared as doubles.

rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2)

Specifically, (dx=x2-x1,x1)? How do I read this?

The whole line is equivalent to:

if (f < 0.0)
{
dx = x2 - x1;
rtb = x1;
}
else
{
dx = x1 - x2;
rtb = x2;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top