简体   繁体   English

将单行if语句拆分为多行if语句

[英]Split single line if statement into multiline if statements

I am trying to split single line if statement into multiline if statement with the same meaning. 我试图将具有相同含义的单行if语句拆分为多行if语句。 I have: 我有:

 if(a || (b && c))
 {
    /* do smt */
 } 

but would like to change it to something like if I would have 但想将其更改为类似

if(a && b && c)
{
  /*do smt*/
}

with the same meaning of 具有相同的含义

if(a)
{
  if(b)
  {
     if(c)
     {
         /* do smt */
     }
  }
}

Thanks! 谢谢!

Boolean algebra can turn this condiiton 布尔代数可以满足这一条件

a || || (b && c) into (b && c)变成

(a || b) && (a || c) (a || b)&&(a || c)

so you can do somthing like: 因此您可以执行以下操作:

if(a || b)
 {
    if(a || c){
    /* do smt */
 } 
}

You can't really do this without repeating the body of the if block. 如果不重复if块的主体,则无法真正做到这一点。 You can transform a || b 您可以转换a || b a || b to !(!a && !b) , but while this uses an && , you can't split this up into nested if statements due to the surrounding !(...) . a || b!(!a && !b) ,但是虽然它使用&& ,但是由于周围!(...)原因,您无法将其拆分为嵌套的if语句。

Why do you want to do this in the first place? 为什么首先要这样做? My assumption would be that the three conditions a , b and c are very long and/or complex. 我的假设是,三个条件abc都很长和/或很复杂。 If this is the case, I'd suggest one of the following: 如果是这种情况,我建议以下其中一项:

  • declare three boolean variables with descriptive names and use those in the condition 声明三个具有描述性名称的布尔变量,并在条件中使用它们

     boolean isLoggedIn = // some really long data base lookup boolean isGuest = // more data base stuff boolean guestCanEdit = // a complex boolean expression if (isLoggedIn || (isGuest && guestCanEdit)) { ... 
  • define three methods performing the above checks and use those in the if 定义执行上述检查的三种方法,并在if

     if (isLoggedIn(user) || (isGuest(user) && guestCanEdit(topic))) { ... 

Note, however, that the first version does not use short-circuiting, ie all the conditions will be evaluated, even if the first is already true or the second is false. 但是请注意,第一个版本不使用短路,即,即使第一个已经为真或第二个为假,也将评估所有条件。 This should be avoided if any of the conditions in computationally expensive or eg if the third check is only possible if the second succeeds (eg after a null check). 如果任何条件在计算上昂贵,或者例如仅在第二次检查成功(例如在null检查之后)时才可能进行第三次检查,则应避免这种情况。


Concerning your comment : The condition 关于您的评论 :条件

if (list.isEmpty() || 
        (!list.isEmpty() && list.getLast().compareTo(heap.peek().value) <= 0))

is not really that long, and I would not suggest any of the above methods for this, as it will not get much shorter that way. 是不是真的那么长,我不会建议任何的这个上面的方法,因为它不会得到更短的方式。 But you can shorten it, because the b part is redundant. 但是您可以缩短它,因为b部分是多余的。 Due to the short-circuiting of || 由于||短路 , (b && c) are only evaluated if !a , and since your b is !a , you can shorten it to just a || c (b && c)仅在!a求值,并且由于您的b !a ,您可以将其缩短a || c a || c

if (list.isEmpty() || list.getLast().compareTo(heap.peek().value) <= 0)

If your goal is to count how many times compareTo is called, you can use this: 如果您的目标是计算调用compareTo次数,则可以使用以下代码:

if (! list.isEmpty() && list.getLast().compareTo(heap.peek().value) <= 0)

Now this is just b && c , with the a part entirely missing. 现在,这只是b && c ,用a部分完全丢失。 Note that this is not equivalent to a || (b && c) 请注意,这等于a || (b && c) a || (b && c) anymore, but in this case that's a good thing, because due again to the short-circuiting, compareTo would actually not be called in a || c a || (b && c)了,但是在这种情况下,这很好,因为再次由于短路,实际上将不会a || c调用compareTo a || c if a already evaluated to true . a || c如果a已经评估为true

Yes you can do this, but perhaps you shouldn't. 是的,您可以执行此操作,但也许不可以。


if (a && b) statement;

is exactly equivalent to 完全等同于

if (a){
    if (b){
        statement;    
    }
}

But the same can't be said for if (a || b) : you'd need to write statement; 但是对于if (a || b)则不能这样说:您需要编写statement; in more than one place: 在一个以上的地方:

if (a){
    statement;
} else if (b){
    statement;
}

That said, || 就是说, || does distribute across && , even with the short-circuiting property: 确实分布&& ,即使具有短路特性:

So 所以

if (a || b && c/*redundant parentheses removed*/){
    statement;
}

can be written as 可以写成

if ((a || b) && (a || c)){
    statement;
}

which, from above, is 从上面是

if (a || b){
    if (a || c){
        statement;
    }
}

which, although unnecessarily obfuscated, is what you want. 尽管您不必要混淆,但这正是您想要的。

The need for this is not very clear, but you could do this: Make it in a function: 对此的需求不是很清楚,但是您可以执行以下操作:在函数中进行操作:

function codeToDo() {
  // your code to execute on condition
}

if (a) {
  codeToDo();
else if (b) {
  if (c) {
    codeToDo();
  }
}
if(a || b)
 {
     if(a || c){
    /* do smt */
     }
 }

To further handle if(a||b) part, apply: 要进一步处理if(a||b)部分,请应用:

if(a){
   /*do task1*/
}else if(b){
   /*do task1*/
}

Note that in the if(a) as well as else if(b) , you are running same code;ie task1. 注意,在if(a)else if(b) ,您正在运行相同的代码;即task1。

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

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