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

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
Programming Functions: Passing Parameters by Reference.
Chapter 4 Loops Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Computer Science 1620 Loops.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
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 Chapter 3 Selections.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 4 Loops.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 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 Chapter 3 Selections.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 4 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 06 Title: Iterative/Repetitive Control Structures Reference: MalikFarrell, chap 1, Liang Ch 4.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to Computer Programming
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Chapter 4 Loops DDC 2133 Programming II.
Chapter 4 Loops Case Studies
Chapter 4 Control structures and Loops
Repetition Control Structure
Chapter 5 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Looping III (do … while statement)
Chapter 4: Loops and Iteration
Presentation transcript:

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; }