Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be 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.

How would you rewrite this using a switch statement?

String checkZero(int x)

{

String s;

if (x == 0)

s = "zero";

else

s = "non-zero";

return s;

}

Which do you think is the best choice for this method? Why?

Update:

How would this work for Java?

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Switch statements are for many options.

    If you mean C/C++, a tertiary operator will be the shortest.

    s = x==0?"zero":"non-zero";

    Still smaller:

    s = x? "non-zero" : "zero";

    Smallest:

    s = (x?"non-":"") + "zero";

    Have fun!

    .

    Source(s): TaZ
  • 1 decade ago

    String checkZero(int x){

    switch(x){

    case(0):

    //whatever

    default:

    //whatever

    }

    return s;

    }

    doesn't really matter what way you do it, i find switch statements are cleaner to look at

Still have questions? Get your answers by asking now.