/////////////////////////////////////////////////////// // // loops2.cpp // // Started 1/6/97 by Jeff Ondich // Last modified: 9/16/99 // // 1. This program has two more or less unrelated parts. // The first part demonstrates a "while" loop, which is // discussed in Chapter 2 of Savitch starting on page 80. // The second part demonstrates a "for" loop, which is // not discussed until Chapter 7, but which we will talk // about starting on Monday. // // Read through this program and predict its behavior. // The program comes in two parompile it and run it. // // 2. Try to make the program taunt the user if the user // guesses a number outside the range 1-10. // // 3. Make the loop in Part II count downwards from 10 // to 1. Make it count upwards by threes from 1 to 19. // // 4. How would you re-write the body of the "for" loop // if you left the "update" section of the "for" // statement blank (i.e. "for( int i=0; i < 10; )") // but wanted the loop as a whole to behave just as // it did originally? // // 5. Note that the variable "i" is declared inside the // "for" loop. How does the compiler feel about it if // you try to use the variable "i" after the // loop is done? // //////////////////////////////////////////////////////// #include int main( void ) { // // Part I. Let's play a game. // int secretNumber, guess; secretNumber = 3; cout << "I'm thinking of a number between 1 and 10. Your guess? "; cin >> guess; while( guess != secretNumber ) { cout << "Nope. Try again: "; cin >> guess; } cout << "Huzzah! You are victorious." << endl << endl; // // Part II. Up and down. // int i = 17; cout << "Here's i: " << i << endl; i++; cout << "Now here's i again: " << i << endl; cout << "What do you think i++ does?" << endl << endl; for( i=0; i < 10; i++ ) cout << i << endl; return( 0 ); }