开发者

Ideal If statement structure for two boolean values

开发者 https://www.devze.com 2023-02-17 05:19 出处:网络
I wasn\'t quite sure what to search under - but I was looking for an elegant way if possible to structure an if statement that uses two Boolean values that could easily output all four possibilities.

I wasn't quite sure what to search under - but I was looking for an elegant way if possible to structure an if statement that uses two Boolean values that could easily output all four possibilities.

Variables:

bool a = true;
bool b = true;

I wasn't sure if there was a best practice in terms of checking both for negativity - then continuing on etc.

Very hastily written example:

if(!a && !b)
{
   //Output (-,-)
}
else
{
   if(a || b)
   {
       if(a)
       {
           //Output (+,-)
       }
       else
       {
           //Output (-,+)
       }
   }
   else
   {
       //Output (+,+)
   }
}

Sorry for all the gullwings ( { } ) I am a bit of a fo开发者_StackOverflow中文版rmatting junkie. Anyways - thanks for any suggestions!


It depends on how you define elegant.

So I;m going to go with my own definition:

  1. Symmetric.
  2. Readable.
  3. Avoids unnecessary complexity.

With that in mind, I'd just go for:

if( a ) {
  if( b ) {
     ...
  } else {
     ...
  }
} else {
  if( b ) {
     ...
  } else {
     ...
  }
}

It isn't any less verbose than your idea but at least it's crystal clear what is meant there.

Having said that, I find control structures like this highly suspicious. You can probably either:

  1. Use a (two-dimensional) constant array to fetch a value, if all you do is assign a value to a variable.
  2. Rephrase the whole block to avoid redundantly calling similar code. Maybe factor out one of the checks into a function.
  3. You may not need two separate booleans at all, a single variable with 4 possible values would be more expressive and you could then use a switch/case structure.


I'm not fully sure I get what you're asking... do you just want an if statement to cover all four possibilities?

If so, here's one simple way to do this:

if (a && b)
    // Output (+, +)
else if (!a && b)
    // Output (-, +)
else if (a && !b)
    // Output (+, -)
else
    // Output (-, -)

If this isn't what you're looking for, let me know and I'll take down this post.


Ideally you wouldn't use if at all...

case a, b of
  true, true => "+,+"
| false, true => "-,+"
| true, false => "+,-"
| false, false => "-,-"

If you want to violate your corporate C coding standard and test your C compiler's optimizer you can approximate this in C with

switch (((!!a) * 2) + (!!b))
{
  case 3: \\ "+,+"
    break;
  case 2: \\ "+,-"
    break;
  case 1: \\ "-,+"
    break;
  case 0: \\ "-,-"
    break;
}


Static analysis can be used to improve on @templatetypedef answer. I personally prefer this as it is more succinct then @biziclop answer

    if (a && b) {
      // Output (+, +)
    } else if (a) {
      // Output (+, -)
    } else if (b) {
      // Output (-, +)    
    } else {
      // Output (-, -)
    }

And can be generalised to more than two variables, e.g.

if (a && b && c)  {

} else if (a && b) {

} else if (a && c) {

} else if (b && c) {

} else if (a) {

} else if (b) {

} else if (c) {

} else {

}


Following snippet is unmaintainable. Anyway

Output(strings[(a ? 2 : 0) + (b ? 1 : 0)])


I tried the following as an example, which seems to work:

result = (name & id) ? "(+,+)" : 
         (!name & !id) ? "(-,-)" : 
         (name & !id) ? "(+,-)" : "(-,+)";

However - I don't know if there is any way to output the values rather than setting a variable to contain them (using the ternary operators).


I think you want this:

char table[2] = {'-','+'};
printf("(name%c,id%c)",table[!!name],table[!!id]);

cheers!

0

精彩评论

暂无评论...
验证码 换一张
取 消