Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

Similar presentations


Presentation on theme: "1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved."— Presentation transcript:

1 1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

2 Loops Break and continue statements Software development process Algorithms 2 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

3 At the end of this topic, students should be able to: Correctly use a while statement in a C++ program Correctly use break and continue statements in a C++ program Correctly use a do statement in a C++ program Correctly use a for statement in a C++ program Describe the stages of software development Create simple algorithms to solve computing problems and create UML activity diagrams to describe their algorithms 3 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

4 The while statement allows the program to execute the same statement multiple times. while ( count <= MAX ) { cout << count << ‘ ‘; count ++; } is count <= MAX ? cout << count << ‘ ‘; count++; true this is called the limit Inside the loop, you must always do something to the variable being tested, so that the program will eventually exit the loop false 4 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

5 // Using a While Statement with a limit // main class declaration int main ( ) { const int MAX = 5; int count = 1; while ( count <= MAX ) { cout << count; count++; } while the limit has not been reached … change the number being compared to the limit 5 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

6 // Using a While Statement with a sentinel int main ( ) { int value = 1; while ( value != 0 ) { cout << "Enter in a digit between 0 and 9, use 0 to quit:"; cin >> value; cout << "You typed " << value; } cout << "Goodbye!"; } the sentinel in this case is 0 get another value to compare with the sentinel 6 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

7 ... const int MAX = 5; cout << “Enter an integer between 0 and 5; cin >> input; while ( input MAX ) { cout << “Invalid input. Try again”; cin >> input; }... 7 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

8 // Nested While int main ( ) { const int COUNT = 3; const int TIMES = 2; int valueOne, valueTwo; valueOne = 0; while ( valueOne <= COUNT ) { cout << "In outer loop, valueOne = " << valueOne; valueTwo = 0; while ( valueTwo <= TIMES ) { cout << " In inner loop, valueTwo = " << valueTwo; valueTwo++; } valueOne++; } cout << "Goodbye!"; } 8 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

9 break – breaks immediately out of a loop/switch. continue – skip the rest of the loop and go back and go through the loop another time. 9 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

10 do { count++; cout << count; } while ( count < LIMIT); count = count +1; cout << count; is count < LIMIT ? true false in a do statement, the loop will always be executed at least once! 10 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

11 Validating Input A common programming problem is to do something, and then ask the user if they want to do it again. If the user answers “yes” you do it again. If the user answers “no” you stop. If the user answers neither, tell him to try the answer again. 11 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

12 Write the code ….. 12 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

13 First – our up front boilerplate #include using namespace std; int main ( ) { 13 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

14 Second – declare the variables we will use int number; // a user entered value char yesNo; // store use response to do it again bool okay = true, notDone = false; // some flags 14 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

15 Prompt user For a value Get input From the user Display the result number 15 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

16 // prompt, get user input, and display it cout << <\nEnter an integer value: “; cin >> number; cout << “\nYou typed the number “ << number; 16 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

17 Prompt user For a value Get input From the user Display the result Prompt “Play Again?” Get input From the user Input Valid ? Display an error message no yesNo 17 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

18 // prompt to play again – make sure response is valid cout << <\nDo you want to play again(y or n)?: “; cin >> yesNo; if(cin.rdbuf()->in_avail()!=0) cin.ignore(80, ‘\n’); // get rid of extra characters // see if response is valid – print a message if its not okay = (yesNo == ‘y’ || yesNo == ‘n’); if (!okay) cout << “\nSorry, but that is not a valid response.”; 18 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

19 // prompt to play again – make sure response is valid cout << <\nDo you want to play again(y or n)?: “; cin >> yesNo; if(cin.rdbuf()->in_avail()!=0) cin.ignore(80, ‘\n’); // get rid of extra characters // see if response is valid – print a message if its not okay = (yesNo == ‘y’ || yesNo == ‘n’); if (!okay) cout << “\nSorry, but that is not a valid response.”; Then here is the first loop – stay in loop until there is a valid response do { } while (!okay); 19 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

20 Prompt user For a value Get input From the user Display the result Prompt “Play Again?” Get input From the user Input Valid ? Display an error message no Play again ? yesno end 20 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

21 #include using namespace std; int main ( ) { int number; char yesNo; bool okay = true, notDone = true; do // main loop { cout << "\nEnter an integer value: "; cin >> number; cout << "\nYou types the number " << number; do { cout << "\nDo you want to play again (y or n)? "; cin >> yesNo; if(cin.rdbuf()->in_avail()!=0) cin.ignore ( 80, '\n'); // get rid of extra characters okay = ( yesNo == 'y' || yesNo == 'n'); if ( !okay ) cout << "\nSorry, that is not a valid response.” } while (!okay); if (yesNo == 'n') notDone = false; } while (notDone); } 21 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

22 Apply this code to the guessing game in the following slide Practice 22 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

23 int main ( ) { // We will generate numbers between 0 and 10 const int MAX = 10; int answer, guess; randomize ( ); answer = rand( ) % 11; cout << "I am thinking of a number between 0 and "; cout << MAX << " What is it? "; cin >> guess; if(cin.rdbuf()->in_avail()!=0) cin.ignore(80,’\n’); if ( guess == answer ) cout << "Congratulations. You guessed it.\n"; else { cout << "That is not correct, sorry. "; cout << "The number was " << answer << ‘\n’; } cout << "Thanks for playing.\n"; } 23 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

24 The for statement is best used when you know exactly how many times you want to execute the loop. for ( ) { cout << count; } initialization evaluationincrement int count = 0;count < LIMIT;count++ Initialize evaluate condition body of loop increment 24 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

25 The comma operator is used to evaluate a list of expressions, but only return the value in the last expression. goodNum = (first = 2, second = first + 1); this expression is evaluated first then this expression is evaluated and the result is assigned to goodNum. 25 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

26 establish the requirements create the design implement the code test the implementation 26 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

27 The steps that we come up with to solve a problem are called an algorithm. An algorithm is like a recipe in a cookbook. Once you come up with an algorithm to solve a particular problem, you can apply it to other similar problems. 27 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

28 You just purchased a new computer that cost $1000.00. You did not have to make a down payment, and your payments are $50.00 a month. The interest rate on your purchase is 18% per year. How many payments will you have to make to pay off the loan and what is the total interest that you will pay over the life of the loan. Each month when you make a payment, your payment first pays the interest for that month. The monthly interest rate is 1.5%. Once the interest is paid, the balance of you payment goes towards the balance of the loan. 28 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

29 Write down everything you know about the problem. Loan amount = 1000.00 Monthly Payment = 50.00 Interest Rate = 18% 29 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

30 Write down what you are looking for Months to Pay Total Interest 30 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

31 Each month when you make a payment, your payment first pays the interest for that month. The monthly interest rate is 1.5%, so the first month the interest will be 1.5% x $1000.00, or $15.00. Once the interest is paid, the balance of you payment goes towards the balance of the loan. The first month you will have $35.00 left after paying the interest, so subtracting this from the loan balance will make the new balance $965.00. The next month, repeat the process, starting with the new balance, $965.00. 31 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

32 Pseudo code is an English-like description of the programming steps taken to solve a problem. Write the pseudo code required to calculate the new balance each month interest = balanceDue x monthlyRate paymentBalance = payment – interest balanceDue = balanceDue - paymentBalance 32 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

33 We can create what is called an Activity Diagram to show these steps pictorially. interest = balanceDue x monthlyRate paymentBalance = payment - interest balanceDue = balanceDue – paymentBalance 33 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

34 We know that we will do this same calculation each month, for some number of months. This suggests that we put the code we just developed into a loop of some kind. The pseudo code to describe this might be something like while the loan is unpaid, do the following: interest = balanceDue x monthlyRate paymentBalance = payment – interest balanceDue = balanceDue - paymentBalance 34 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

35 interest = balanceDue x monthlyRate paymentBalance = payment - interest balanceDue = balanceDue – paymentBalance is the loan paid off? yes no 35 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

36 To complete the pseudo code and the diagram, we just need to take care of initializing some things: interest = balanceDue x monthlyRate paymentBalance = payment - interest balanceDue = balanceDue – paymentBalance is the loan paid off? yes no monthlyRate =.015 balanceDue = 1000. payment = 50. 36 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

37 double balanceDue = 1000.00; double monthlyRate =.015; double payment = 50.00; double interest = balanceDue * monthlyRate; double paymentBalance = payment – interest; balanceDue = balanceDue – paymentBalance; What kind of loop? If there is a balance on the loan, we must go through the loop. If not, we can quit. 37 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

38 double balanceDue = 1000.00; double monthlyRate =.015; double payment = 50.00; double interest = balanceDue * monthlyRate; double paymentBalance = payment – interest; balanceDue = balanceDue – paymentBalance; do { } while (balanceDue > 0); 38 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

39 Well, what did the problem ask for? * The number of months it takes to pay off the loan * the total interest paid during the life of the loan Nope …. we haven’t calculated these yet. How would you fix the program to add these calculations? 39 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

40 double balanceDue = 1000.00; double monthlyRate =.015; double payment = 50.00 double totalInterest = 0.0; int monthsToPay = 0; double interest = balanceDue * monthlyRate; totalInterest += interest; monthsToPay++; double paymentBalance = payment – interest; balanceDue = balanceDue – paymentBalance; do { } while (balanceDue > 0); 40 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

41 Well …. almost. We probably ought to print out the results. 41 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

42 double balanceDue = 1000.00; double monthlyRate =.015; double payment = 50.00 double totalInterest = 0.0; int monthsToPay = 0; double interest = balanceDue * monthlyRate; totalInterest += interest; monthsToPay++; double paymentBalance = payment – interest; balanceDue = balanceDue – paymentBalance; do { } while (balanceDue > 0); cout << Months to pay off loan = “ << monthsToPay << “\n”; cout << “Total interest = “; cout.setf (ios::fixed); cout.setf (ios::showpoint); cout.precision (2); cout << totalInterest << “\n”; 42 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

43 43 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

44 double balanceDue = 1000.00; double monthlyRate =.015; double payment = 50.00 double totalInterest = 0.0; int monthsToPay = 0; double interest = balanceDue * monthlyRate; totalInterest += interest; monthsToPay++; double paymentBalance = payment – interest; balanceDue = balanceDue – paymentBalance; do { } while (balanceDue > 0); cout << Months to pay off loan = “ << monthsToPay << “\n”; cout << “Total interest = “; cout.setf (ios::fixed); cout.setf (ios::showpoint); cout.precision (2); cout << totalInterest << “\n”; #include using std::cin; using std::cout; using std::ios; int main( ) { { 44 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.

45 Write a program that displays the multiplication tables between 2 and 12. Display the output in columns, so that it all appears on one screen. Your table should be nicely lined up like the following: 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 etc... 45 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.


Download ppt "1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved."

Similar presentations


Ads by Google