/*
* @topic Q00607 2/6/2014 Flow control
* @brief in-class demo -- endless for(;;) loop
*/
#include <iostream>
#include <string>
using namespace std;
void pause()
{
//----------------------------------------------
// prevent window from closing before exiting:
//----------------------------------------------
char ch; // ch is a variable that holds a character
// curious \n is a "line feed" control character,
// it moves the cursor to the next line:
std::cout << "\nEnter X to Exit: ";
std::cin.ignore( 1, '\n' ); // extract and discard characters
std::cin.clear(); // this clears cin state
std::cin.get( ch ); // this reads a character from the keyboard,
// and pauses the program from before exiting
}
int main()
{
// Another example of a "while" loop:
int count = 5;
while ( count > 0 ) {
cout << count << ' ';
count = count - 1;
}
cout << '\n';
// Similar loop, but using a "for" statement:
for ( count = 5; count > 0; count = count - 1 ) {
cout << count << ' ';
if ( count == 2 ) {
break;
}
}
// The for(;;) construct is pronounced as "forever"
// This is an endless loop. The user, however, can break out
// of the endless loop by entering the required input:
for(;;) {
char ch;
cout << "Would you like to exit? Type y if YES: ";
cin >> ch;
if ( ch == 'y' ) {
return 0;
}
}
pause();
}