2015-01-07 45 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... 
}