### dangling-else problem

(Dangling-else Problem) State the output for each of the following when x is 9 and y is 11 and when x is 11 and y is 9. The compiler ignores the indentation in a C++ program. The C++ compiler always associates an else with the previous if unless told to do otherwise by the placement of braces {}. On first glance, you may not be sure which if and else match, so this is referred to as the “dangling-else” problem. We eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply indentation conventions you’ve learned.]

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` `````` if ( x < 10 ) if ( y > 10 ) cout << "*****" << endl; else cout << "#####" << endl; cout << "\$\$\$\$\$" << endl; b) if ( x < 10 ) { if ( y > 10 ) cout << "*****" << endl; } else { cout << "#####" << endl; cout << "\$\$\$\$\$" << endl; } Solution: a) x = 9, y = 11 ***** \$\$\$\$\$ x = 11, y = 9 \$\$\$\$\$ b) x = 9, y = 11 ***** x = 11, y = 99 ##### \$\$\$\$\$``````
And what is the question?
repost it with proper indentation
my confusion is like in the first part if (9<10) the if part is empty for x portion so shouldn't print anything and (11>10) for the y portion so it should print the first line of else as c++ rule if no brackets are applied so the output should be "#####" but it is "*****" and "\$\$\$\$\$" which is completely opposite of what i think.
a)
 ``12345678910111213`` ``````if ( x < 10 ) { if ( y > 10 ) { cout << "*****" << endl; } else { cout << "#####" << endl; } } cout << "\$\$\$\$\$" << endl;``````

b)
 ``123456789101112`` ``````if ( x < 10 ) { if ( y > 10 ) { cout << "*****" << endl; } } else { cout << "#####" << endl; cout << "\$\$\$\$\$" << endl; }``````

Topic archived. No new replies allowed.