Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 01360972001 Chapter 4 Loops.

Similar presentations


Presentation on theme: "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 01360972001 Chapter 4 Loops."— Presentation transcript:

1 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 01360972001 Chapter 4 Loops

2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2 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?

3 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 01360972003 Objectives F To write programs for executing statements repeatedly using a while loop (§4.2). F To develop programs for GuessNumber and SubtractionQuizLoop (§§4.2.1- 4.2.2). 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).

4 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4 while Loop Flow Chart while (loop-continuation-condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { cout << "Welcome to C++!\n"; count++; }

5 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5 Trace while Loop int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Initialize count animation

6 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6 Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } (count < 2) is true animation

7 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7 Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Print Welcome to C++ animation

8 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8 Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Increase count by 1 count is 1 now animation

9 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9 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

10 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10 Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Print Welcome to C++ animation

11 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11 Trace while Loop, cont. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } Increase count by 1 count is 2 now animation

12 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12 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

13 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13 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

14 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14 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

15 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15 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;

16 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16 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;

17 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17 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++; }

18 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18 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; }

19 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19 SubtractionTutorLoop.cpp

20 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20 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; }

21 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21 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

22 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22 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;

23 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 23 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; }

24 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 24 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.

25 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 25 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; }


Download ppt "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 01360972001 Chapter 4 Loops."

Similar presentations


Ads by Google