Kae
Kae

Reputation: 279

Conditioning for the four cases of the signs of two numbers

I have two numbers A and B. I would like to condition for the four cases of the signs of these two numbers. We could do

if ((A >= 0) && (B >= 0)){
  // Do something
};
if ((A >= 0) && (B < 0)){
  // Do something
};
if ((A < 0) && (B >= 0)){
  // Do something
};
if ((A < 0) && (B < 0)){
  // Do something
};

One could produce a function that outputs different values in each case and then use a switch{ case:} statement. For the functions I have thought this doesn't improve the number of comparisons, so there is no much gain.

Which way is recommended for doing this conditioning?

Well, I guess some of those if-s could be put inside else-s of the others so that not all the conditions have to be evaluated always.

Upvotes: 0

Views: 84

Answers (1)

Neil Kirk
Neil Kirk

Reputation: 21763

if (A >= 0)
{
    if (B >= 0)
    {
    }
    else // B < 0
    {
    }
}
else // A < 0
{
    if (B >= 0)
    {
    }
    else // B < 0
    {
    }
}

Upvotes: 3

Related Questions