//illustrates the dangling else problem //If you have two if statements and one else and don't use { } blocks //the else will always be associated with the inner if statement //9/12/18 #include using namespace std; int main() { int x, y ; cout << "Enter values for integers x and y "; cin >> x >> y; //First nested if structure cout << "Output from the first nested if structure\n"; if ( x < 10 ) if ( y > 10 ) cout << x << " " << y << "*****" << endl; //whether indented or not, this else matches with the inner if statement //so it should be indented else cout << x << " " << y << "#####" << endl; //Second nested if structure cout << "\nOutput from the second nested if structure\n"; if ( x < 10 ) if ( y > 10 ) cout << x << " " << y << "*****" << endl; else //this else block is also matched with the inner if statement { cout << x << " " << y<< "%%%%%" << endl; cout << "&&&&&" << endl; } //Third nested if structure cout << "\nOutput from the third nested if structure\n"; if ( x < 10 ) { if ( y > 10 ) cout << x << " " << y<< "*****" << endl; } //end outer if block //the else block matches the outer if statement because we //used a block in the outer if statement body else { cout << x << " " << y << endl << "^^^^^" << endl; cout << "@@@@@" << endl; } //end if-else block return 1; } //end main() /* First run Enter values for integers x and y 9 11 Output from the first nested if structure 9 11***** Output from the second nested if structure 9 11***** Output from the third nested if structure 9 11***** Process returned 1 (0x1) execution time : 3.557 s Press any key to continue. */ /* second run Enter values for integers x and y 11 9 Output from the first nested if structure Output from the second nested if structure Output from the third nested if structure 11 9 ^^^^^ @@@@@ Process returned 1 (0x1) execution time : 6.287 s Press any key to continue. */ /* Third run Enter values for integers x and y 9 9 Output from the first nested if structure 9 9##### Output from the second nested if structure 9 9%%%%% &&&&& Output from the third nested if structure Process returned 1 (0x1) execution time : 2.543 s Press any key to continue. */ /*Fourth run Enter values for integers x and y 11 11 Output from the first nested if structure Output from the second nested if structure Output from the third nested if structure 11 11 ^^^^^ @@@@@ Process returned 1 (0x1) execution time : 3.136 s Press any key to continue. */