///////////////////////////////////////////////////////////////////// // // loops1.cpp // // Started 4/1/98 by Jeff Ondich // Last modified: 9/16/99 // // This program demonstrates a simple "while" loop. // 1. Compile and run this program. Yee-hah! Take a look // at the "while" loop and see if you can make sense out of // how it works. // // 2. What do you expect to happen if you leave out the // "i = i + 1" line? Try removing that line, saving, // recompiling, and executing. Were you right? (By // the way, you can stop a program running out of control // by typing a CTRL-C. That is, hold down the CTRL key // while typing C.) // // 3. Put the i = i+1 back in. What do you expect to happen if // remove the "i = 1" line? Try it and see. // // 4. Try changing the program to deliver only odd-numbered greetings. // Then try delivering only even-numbered greetings. // // 5. A little terminology. A "loop" is a program structure that // allows you to do something over and over. When you add // one to a variable (e.g. i = i + 1), you are "incrementing" // that variable. The question asked in a while loop or // other loops (in the case below the question is // "i <= nGreetings", or in English, "is the value of variable // i less than or equal to the value of variable nGreetings?") // is called the "loop condition" or "loop test." // ///////////////////////////////////////////////////////////////////// #include int main( void ) { int i, nGreetings; cout << "How many greetings do you want? "; cin >> nGreetings; i = 1; while( i <= nGreetings ) { cout << "Howdy #" << i << endl; i = i + 1; } return( 0 ); }