Consider the code segment written below in C++ : if (count < 10)//if#1
Consider the code segment written below in C++ :
if (count < 10)//if#1
if ((count%4) ==2)//if#2
cout<<" condition;white/n";
else//(Indentation is wrong)
cout<<" condition is:tan/n";
There are 2 if statements and one else.
To which if, the else statement belong?
Show answerHide answer
Correct answer: (B) It belongs to if # 2
Explanation
In C++ an else pairs with the nearest preceding unmatched if (the dangling-else rule), whatever the indentation. So the else belongs to if#2.