/*
* @topic Q00710 2/27/2014 for vs. while loop demo
* @brief these loops are not the same!
*/
#include <iostream>
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()
{
// for loop that prints 1 2 4 5
int idx = 1;
for( ; idx < 6 ; idx = idx + 1 ) {
if ( idx == 3 ) {
continue;
}
std::cout << " " << idx;
}
std::cout << "\n";
// How about the while loop equivalent of the above?
idx = 1;
while ( idx < 6 ) {
if ( idx == 3 ) {
idx = idx + 1; // NOTE: extra code to avoid endless loop
continue;
}
std::cout << " " << idx;
idx = idx + 1;
}
pause();
return 0;
}