// Filename: BREXIT.CPP
// Demonstrates both break and return
#include <iostream.h>
void main()
{
int count=0;
char ans;
// The following loop prints "Hi" five times only
// as long as the user keeps requesting the word.
// Whether the user terminates the loop early or
// not, the program continues executing.
while (count < 5)
{
cout << "Hi" << endl;
cout << "Do you want to see it again (Y/N)? ";
cin >> ans;
if ( ans == 'N' || ans == 'n')
{
break;
}
count++;
}
cout << endl;
// The following loop prints "Bye" five times only
// as long as the user keeps requesting the word.
// The difference here from the previous section
// is that the entire program, not just the loop,
// terminates if the user requests termination.
count = 0;
while (count < 5)
{
cout << "Bye" << endl;
cout << "Do you want to see it again (Y/N)? ";
cin >> ans;
if (ans == 'N' || ans == 'n')
{
return; // Exit current function
}
count++;
}
// The following cout executes ONLY if the user let
// the previous loop execute to its natural termination
cout << "That's all, folks!" << endl;
return; // Return to QuickWin
}
Tidak ada komentar:
Posting Komentar