(Last Revision -- 03/12/00)
if-Statement and else-Statment with Nesting
#include <iostream.h>
#include <string>
/* illustrates use of nested if-else statements */
int main(void)
{
string response;
cout << endl ;
cout << "Which do prefer, dogs or cats?[dogs/cats]: ";
cin >> response;
if ("dogs" == response)
{ /* This code is executed if the first
response is "dogs." */
cout << endl ;
cout << "Do you like big dogs?[yes/no]: " ;
cin >> response;
if ("yes" == response)
{ /* This code is executed if the first
response is "dogs" and the second
response is "yes." */
cout << endl ;
cout << "Big dogs can be a real pleasure." << endl ;
cout << "I recommend you get a mastiff." << endl ;
cout << endl ;
}
else
{ /* This code is executed if the first
response is "dogs" and the second
response is "no.") */
cout << endl ;
cout << "A small pet is often a wise choice." << endl ;
cout << "You ought to get a chihuahua." << endl ;
cout << endl ;
}
} /* end of outer if-statement */
else
{ /* This code is executed if the first
response is "cats." */
cout << endl ;
cout << "Do you like long-haired cats?[yes/no]: " ;
cin >> response;
if ("yes" == response)
{ /* This code is executed if the first
response is "cats" and the second
response is "yes." */
cout << endl ;
cout << "Long-hairs sure are beautiful." << endl ;
cout << "I recommend you get a Persian." << endl ;
cout << endl ;
}
else
{ /* This code is executed if the first
response is "cats" and the second
response is "no." */
cout << endl ;
cout << "A short-hair is more practical." << endl ;
cout << "How about a tabby cat?" << endl ;
cout << endl ;
}
} /* end of outer else-statement */
return 0;
}