Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which
of the statements below is true regarding this structure?
if (condition1)
if (condition2)
statement1;
else statement2;
a) syntactically it is invalid to have more if clauses than else clauses
b) statement2 will only execute if condition1 is false and condition2 is false
c) statement2 will only execute if condition1 is true and condition2 is false
d) statement2 will only execute if condition1 is false, it does not matter what condition2 is
e) statement2 will never execute
Answer: b. Explanation: The else clause must be matched to the most recent if statement. Therefore, the else goes with the if statement that tests condition2, not the if statement that tests condition1. So, if condition1 is true and condition2 is false, then statement2 will execute.