Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Motivations Suppose that you need to print a string (e.g., "Welcome to C++!") a hundred times. It would be tedious to have to write the following statement a hundred times: cout << "Welcome to C++!" << endl; So, how do you solve this problem?
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Objectives F To write programs for executing statements repeatedly using a while loop (§4.2). F To develop programs for GuessNumber and SubtractionQuizLoop (§§ ). F To control a loop with the user confirmation (§4.2.3). F To control a loop with a sentinel value (§4.2.4). F To obtain large input from a file using input redirection rather than typing from the keyboard (§4.2.5). F To write loops using do-while statements (§4.3). F To write loops using for statements (§4.4). F To discover the similarities and differences of three types of loop statements (§4.5). F To write nested loops (§4.6). F To learn the techniques for minimizing numerical errors (§4.7). F To learn loops from a variety of examples (GCD, FutureTuition, PrintPyramid, PrimeNumber) (§§4.8, 4.10). F To implement program control with break and continue (§4.9).
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved while Loop Flow Chart while (loop-continuation-condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { cout << "Welcome to C++!\n"; count++; }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Initialize count animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } (count < 2) is true animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Print Welcome to C++ animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Increase count by 1 count is 1 now animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } (count < 2) is still true since count is 1 animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Print Welcome to C++ animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Increase count by 1 count is 2 now animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } (count < 2) is false since count is 2 now animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Trace while Loop int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } The loop exits. Execute the next statement after the loop. animation
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Example: An Advanced Math Learning Tool Write a program that generates two random single digit numbers and ask the user number1-number2 (with number1>number2). The program generates ten questions and reports the number of the correct answers after a student answers all ten questions. The program also displays the time spent on the test and lists all the questions, as shown in sample output. SubtractionTutorLoop.cpp
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Example: An Advanced Math Learning Tool F To generate a random number, use rand() function. F Executing the following three lines will give the same numbers every time you run the program on any particular machine: cout<<rand()<<endl F To generate different numbers every time you run the program: srand(time(0)); cout<<rand()<<endl;
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved SubtractionTutorLoop.cpp #include #include // for time function #include // for the srand and rand functions using namespace std; int main() { int correctCount = 0; // Count the number of correct answers int count = 0; // Count the number of questions long startTime = time(0); while (count < 10) { // 1. Generate two random single-digit integers srand(time(0)); int number1 = rand() % 10; int number2 = rand() % 10;
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved SubtractionTutorLoop.cpp // 2. If number1 < number2, swap number1 with number2 if (number1 < number2) { int temp = number1; number1 = number2; number2 = temp; } // 3. Prompt the student to answer “what is number1 – number2?” cout << "What is " << number1 << " - " << number2 << "? "; int answer; cin >> answer; // 4. Grade the answer and display the result if (number1 - number2 == answer) { cout << "You are correct!\n"; correctCount++; }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved SubtractionTutorLoop.cpp else cout << "Your answer is wrong.\n" << number1 << " - " << number2 << " should be " << (number1 - number2) << endl; // Increase the count count++; } long endTime = time(0); long testTime = endTime - startTime; cout << "Correct count is " << correctCount << "\nTest time is " << testTime << " seconds\n"; return 0; }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved SubtractionTutorLoop.cpp
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Controlling a Loop with User Confirmation char continueLoop = 'Y'; while (continueLoop == 'Y') { // Execute body once // Prompt the user for confirmation cout << "Enter Y to continue and N to quit: "; cin >> continueLoop; }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Ending a Loop with a Sentinel Value Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a value is known as a sentinel value. Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. SentinelValue.cpp
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Ending a Loop with a Sentinel Value SentinelValue.cpp #include using namespace std; int main() { cout << "Enter an int value (the program exits " << "if the input is 0): "; int data; cin >> data; // Keep reading data until the input is 0 int sum = 0;
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Ending a Loop with a Sentinel Value SentinelValue.cpp while (data != 0) { sum += data; // Read the next data cout << "Enter an int value (the program exits " << "if the input is 0): "; cin >> data; } cout << "The sum is " << sum << endl; system(“PAUSE”); return 0; }
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition); TestDoWhile.cpp The statements inside the loop will be executed at least once.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved TestDoWhile.cpp #include using namespace std; int main() { int sum = 0; int data = 0; do { sum += data; // Read the next data cout << "Enter an int value (the program exits " << " if the input is 0): "; cin >> data; } while (data != 0); // Keep reading data until the input is 0 cout << "The sum is " << sum << endl; return 0; }