> >m I correct in stating z=3

No.

z would equal 2

David Maisonave :-)

Cheers!

Solved

Posted on 2005-04-26

I am studying c++ and I am trying to figure out a question for a practice exam. It goes as follows:

Assume you have three int variables x =2, y=6 and z. Choose the value of z in the following:

z = (y/z>0) ? x : y;

am I correct in stating z=3 because the "?" is basically stating "if the previous expression is true (y/z > 0) then z =x, if it is false then y = z ? I am just making sure I am clear on what this expression is saying...

Thank you for your help...

Assume you have three int variables x =2, y=6 and z. Choose the value of z in the following:

z = (y/z>0) ? x : y;

am I correct in stating z=3 because the "?" is basically stating "if the previous expression is true (y/z > 0) then z =x, if it is false then y = z ? I am just making sure I am clear on what this expression is saying...

Thank you for your help...

4 Comments

> >if the previous expression is true (y/z > 0) then z =x

That's right, but x equals 2, and therefore z will equal 2

David Maisonave :-}

But the final value of z would depend on the value of z befoe the expr is evaluated

For example,

if initially z = 1 to 6, then y/z >0, thus, z = x, i.e. z = 2 in this case

if initially z > 6 or z < 0, then, then y/z <= 0, thus z = y, i.e. z = 6 in this case

HTH

Amit

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

TCP/IP Socket - connection close results in data lost | 14 | 98 | |

VS2015 compilation and missing DLLs | 9 | 79 | |

Dynamically allocate memory | 9 | 41 | |

C++ question | 3 | 26 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**6** Experts available now in Live!