Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 5

Comp Sci: y = (x >= 0 ? x : -x); what does it mean?

This is an abbreviated form of an if-else statement, but I can't figure out what it is. The question mark is throwing me off.

Choices:

A. if (x >= 0) y = x;

B. if (x >= 0) y = x; else y = -x;

C. if (x >= 0) y = -x;

D. if (x >= 0) y = -x; else y = x;

I'm almost positive it's B, but can someone please reassure me?

3 Answers

Relevance
  • Anonymous
    8 years ago

    It's a short hand if that returns a value. Here is the long version:

    if (x >= 0) {

    ....y = x;

    } else {

    ....y = -x;

    }

    It's like Ben said, the first value after the question mark is the value if the condition is true; otherwise the other value is used.

  • ?
    Lv 5
    8 years ago

    if x value greater or equals 0 the store x value to y, otherwise store -x value to y

  • Ben
    Lv 7
    8 years ago

    The ternary expression is (condition) ? (value-if-true) : (value-if-false). So yes, b is the correct answer.

Still have questions? Get your answers by asking now.