Print Friendly and PDF
(Dangling­else Problem) State the output for each of the following when x is 9 and y is 1 1 and when x is 1 1 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.]
(Dangling Else Problem) State the output for each of the following when x is 9 and y is 1 1 and when x is 1 1 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.]

a)
if ( x < 10 )
if ( y > 10 )
cout << "*****" << endl;
else
cout << "#####" << endl;
cout << "$$$$$" << endl;
b)
if ( x < 10 )
{i
f ( y > 10 )
cout << "*****" << endl;
}e
lse
{c
out << "#####" << endl;
cout << "$$$$$" << endl;
}

Solution:

a)

x = 9, y = 1 1
*****
$$$$$
x = 1 1 , y = 9

$$$$$
b)

x = 9, y = 1 1
*****
x = 1 1 , y = 99
#####
$$$$$
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: