简体   繁体   English

简化布尔表达式示例

[英]Simplify Boolean Expression example

how to simplify如何简化

if ( this.something == false ) 

this boolean expression ?这个布尔表达式? Actually I want to ask is what is Simplify Boolean Expression ?其实我想问的是什么是Simplify Boolean Expression

You can simply do:你可以简单地做:

if (!this.something)

You can use boolean variables directly:您可以直接使用布尔变量:

Example:例子:

boolean flag = true;
if(flag) {
    //do something
}

Use like following since if expression need a boolean value.使用如下,因为 if 表达式需要一个布尔值。

if (!this.something) {
//
}

You can use ternary operator for more simplification :您可以使用三元运算符进行更简化:

int someVariable = (this.something) ? 0:1;

someVariable will be 1 if this.something is false .如果this.somethingfalse someVariable将为 1 。

Hope this helps.希望这可以帮助。

To simplify boolean expression is to reduce complexity of this expression, with preserving the meaning.简化布尔表达式就是降低表达式的复杂度,同时保留其含义。

In your case:在你的情况下:

if(!this.something)

has the same meaning but it's a little bit shorter.具有相同的含义,但要短一些。

To simplify more complex examples you can use truth tables or Karnaugh maps.为了简化更复杂的示例,您可以使用真值表或卡诺图。

Generally the if statement wants to evaluate whatever is within it into a boolean if通常 if 语句想要将其中的任何内容评估为布尔值 if

boolean something = true;
if(something == true) evaluates to true
if(something != true) evaluates to false

but you can also do但你也可以这样做

if(something) evaluates to whatever something is (true in this case)
if(!something) evaluates to opposite what something is (false in this example)

Also you can simplify if statements in certain cases by using a ternary operator:在某些情况下,您还可以通过使用三元运算符来简化 if 语句:

boolean isSomethingTrue = (something == true) ? true : false;
boolean isSomethingTrue = something ? true : false;
type variable = (condition) ? return this value if condition met : return this value if condition is not met;

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

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