2015-01-07 108 views
-9

只是或否就足够了。 是这个 -关于if语句快速一个

if(condition1) { 
    if (condition2) { 
     //something... 
    } 
} 

同这个 -

if(condition1 && condition2) { 
    //something... 
} 

要求Java中,是有可能,这个逻辑是不同的语言不同?

+0

为什么不调试它? –

+0

是的!逻辑保持不变 –

+0

两者都会给你相同的结果 –

回答

-1

这是一样的。第二个条件仅在第一个条件评估为真时才被测试。如果在第二种情况下只有一个&,它将不会相同!

1

仅当两个条件都为真时,才会输入第一个示例中的inner if块和第二个示例中的if块。

然而,第一实施例允许添加不同的逻辑:

if(condition1) { 
    // here you can add logic that will be executed when condition1 is true, 
    // regardless of condition2 
    if (condition2) { 
     //something... 
    } else { 
     // here you can add logic that will be executed when condition1 is true, 
     // but condition2 is false 
    } 
    // here you can add logic that will be executed when condition1 is true, 
    // regardless of condition2 
} 
1

通常,它们相同。

if(condition1) { 
    //If condition1 is true (irrespective of state of condition 2), I can still do this 
     System.out.println("In here.."); 
    if (condition2) { 
     // I can do this only if condition2 is true. 
    } 
    //If condition1 is true (irrespective of state of condition 2), I can still do this 
System.out.println("and here.."); 
} 



if(condition1 && condition2) { // if one among condtion1 or condtion2 is false, then the if block will not be entered. 
    //something... 
}