Consider the following unformatted nested if-else
Java code
if (condition 1)
if (condition 2)
action 1;
else
action 2;
My question is: according to the Java language specifications, to what if does the else
branch apply?
By hand-reformatting and adding braces, which of these two is correct?
Block 1:
if (condition 1) {
if (condition 2) {
action 1;
} else
action 2;
}
}
Block 2:
if (condition 1) {
if (condition 2) {
action 1;
}
}
else {
action 2;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…