简体   繁体   English

简化 if 条件布尔表达式

[英]Simplify if condition boolean expression

I have this if-condition in my code:我的代码中有这个if-condition

if (a||a&&!b){
// do some stuff
}

with that intial values from my junit test :使用我的junit test中的初始值:

boolean a=true, b = true;

as I recognized later the statement can be simplified to:正如我后来认识到的,该语句可以简化为:

if (a&&b)

becomes green: Assert.assertTrue(a||a&&!b == a&&b);变成绿色: Assert.assertTrue(a||a&&!b == a&&b);

Are there any further simplifications?有没有进一步的简化? How could I have recognized that this boolean expression could be simplified?我怎么知道这个布尔表达式可以简化?

Just check the truth table (I've added parentheses)只需检查真值表(我已添加括号)

  a || (a && (!b))

  a|b|result
  ----------
  T|T|T 
  T|F|T 
  F|T|F 
  F|F|F

As we can see, the formula doesn't depend on b , and can be simplified to only正如我们所见,公式不依赖b ,可以简化为仅

  a

Finally最后

  if (a) {
    // do some stuff
  }

For formulae with many variables when truth table is too long for manual analysis you can use Karnaugh maps as Eomm proposed.对于具有许多变量的公式,当真值表太长而无法手动分析时,您可以使用 Eomm 建议的卡诺图

One common technique used to simplify complex Boolean expressions is Karnaugh Maps .一种用于简化复杂布尔表达式的常用技术是卡诺图 It is relatively easy to learn, and it can help you produce shorter expressions, or even build Boolean expressions from a truth table.它相对容易学习,并且可以帮助您生成更短的表达式,甚至可以从真值表构建布尔表达式。

Karnaugh Map for your expression is very simple - it looks like this:您的表达式的卡诺图非常简单 - 它看起来像这样:

在此处输入图片说明

It simplifies to a , not a && b .它简化为a ,而不是a && b

You could use Karnaugh map for simplify boolean logic. 您可以使用卡诺图来简化布尔逻辑。

With 3 or more variables is the best way 使用3个或更多变量是最好的方法

a || a && !b 

is not equal to不等于

a && b

It is equal to just a .它等于只是a

I suppose that in your JUnit test you used a specific combination of values for a and b where the results match, but that does not mean that the expressions are equivalent—and in fact they aren't.我想在您的 JUnit 测试中,您使用了ab值的特定组合,结果匹配,但这并不意味着表达式是等效的——事实上它们不是。 A quick way to convince yourself of that is checking the combination一种快速说服自己的方法是检查组合

a = true, b = false;

Your original expression clearly yields true for all cases where a == true , but your second expression will yield false whenever b == false .您的原始表达清楚得到true所有情况下, a == true ,但你的第二个表达式将产生false每当b == false

As for a formal proof of equivalence to just a , take the expansion至于等价的,只是一个形式证明a ,采取扩张

a == a && (b || !b)
  == a && b || a && !b

Plugging into your original expression:插入您的原始表达式:

a || a && !b == a && b || a && !b || a && !b
             == a && b || a && !b
             == a

Wolfram Alpha actually does boolean algebra simplification. Wolfram Alpha实际上做了布尔代数简化。 It might take a little converting the answer it gives you to make it compatible with your programming language, but the concept works and checks out fine. 它可能需要一点点转换它给你的答案,使它与你的编程语言兼容,但这个概念有效并且检查得很好。 Here's a link to the page 这是该页面的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM