c++loop statements

21
[email protected] Muhammad Uzair Muhammad Uzair Rasheed Rasheed 2009-CPE-03 2009-CPE-03 UCE&T BZU MULTAN UCE&T BZU MULTAN

Upload: muhammad-uzair-rasheed

Post on 22-Apr-2015

420 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C++loop statements

[email protected]

Muhammad Uzair Muhammad Uzair Rasheed Rasheed

2009-CPE-032009-CPE-03

UCE&T BZU MULTANUCE&T BZU MULTAN

Page 2: C++loop statements

C++ Loop StatementsC++ Loop Statements

Repetition RevisitedRepetition Revisited

Page 3: C++loop statements

ProblemProblem

Using OCD, design and implement a function that, Using OCD, design and implement a function that, given a menu, its first valid choice, and its last valid given a menu, its first valid choice, and its last valid choice, displays that menu, reads a choice from the choice, displays that menu, reads a choice from the user, and returns a value guaranteed to be (i) a valid user, and returns a value guaranteed to be (i) a valid choice from the menu, and (ii) a choice chosen by the choice from the menu, and (ii) a choice chosen by the user. user.

You may assume that the valid menu choices form a You may assume that the valid menu choices form a continuous sequence.continuous sequence.

Page 4: C++loop statements

Preliminary AnalysisPreliminary Analysis

The tricky part is that the function must The tricky part is that the function must return a value thatreturn a value that

(i) is a valid menu choice; and(i) is a valid menu choice; and

(ii) was chosen by the user.(ii) was chosen by the user.

One way to accomplish both goals is to One way to accomplish both goals is to use a loop that displays the menu, use a loop that displays the menu, reads the user’s choice, checks its reads the user’s choice, checks its validity, and if it is not valid, gives them validity, and if it is not valid, gives them another chance...another chance...

Page 5: C++loop statements

BehaviorBehavior

Our function should receive a menu, its Our function should receive a menu, its first valid choice and its last valid first valid choice and its last valid choice. It should repeatedly display choice. It should repeatedly display the menu, read the user’s choice, so the menu, read the user’s choice, so long as the user’s choice is invalid. It long as the user’s choice is invalid. It should then return the user’s choice.should then return the user’s choice.

Page 6: C++loop statements

ObjectsObjects

Description Type Kind NameDescription Type Kind Name

first valid char variable firstChoice choice

last valid char variable lastChoice choice

user’s choice char variable choice

menu string constant MENU

Page 7: C++loop statements

OperationsOperations

Description Predefined? Library? NameDescription Predefined? Library? Name

display strings yes iostream <<read a char yes iostream >>check validity no built-in <=, &&repeat steps yes built-in ?

terminate loop yes built-in ?

return a char yes built-in return

Page 8: C++loop statements

AlgorithmAlgorithm

0. Receive 0. Receive MENUMENU, , firstChoicefirstChoice, , lastChoicelastChoice..

1. Loop1. Loop

a. Display a. Display MENUMENU via via coutcout..

b. Read b. Read choicechoice from from cincin..

c. If c. If firstChoicefirstChoice <= <= choicechoice and and choicechoice <= <= lastChoicelastChoice: :

terminate repetition.terminate repetition.

End loop.End loop.

2. Return 2. Return choicechoice..

Page 9: C++loop statements

OrganizationOrganization

Note that our algorithm terminates the Note that our algorithm terminates the repetition at the repetition at the bottombottom of the loop. of the loop.

To code such loops conveniently and To code such loops conveniently and readably, C++ provides the readably, C++ provides the dodo loop. loop.

Page 10: C++loop statements

CodingCoding

char GetValidMenuChoice(const string MENU,char firstChoice, char lastChoice)

{ char choice; do { cout << MENU; cin >> choice; } while (choice < firstChoice || choice > lastChoice);

return choice;}

Page 11: C++loop statements

DiscussionDiscussion

The do loop tests its condition at the end The do loop tests its condition at the end of the loop, making it useful for any of the loop, making it useful for any problem in which the body of the loop problem in which the body of the loop must be performed must be performed at least onceat least once..

This function seems general enough to This function seems general enough to be reuseable by any menu-using be reuseable by any menu-using program, so it should be stored in a program, so it should be stored in a library.library.

Page 12: C++loop statements

Using the FunctionUsing the Function

Once our function is stored in a library, a Once our function is stored in a library, a programmer can write something like this:programmer can write something like this:

#include “Menu.h”

int main(){ const string MENU = “Please enter:\n” “ a - to do this\n” “ b - to do that\n” “ c - to do the other\n” “--> “;

char choice = GetValidMenuChoice(MENU, ‘a’, ‘c’);

// ...}

Page 13: C++loop statements

TestingTestingPlease enter: a - to do this b - to do that c - to do the other--> sPlease enter: a - to do this b - to do that c - to do the other--> qPlease enter: a - to do this b - to do that c - to do the other--> a...

Page 14: C++loop statements

NoteNoteIf we wish to display an error message, that changes our algorithm...If we wish to display an error message, that changes our algorithm...

Such a message should only be displayed if the user enters an invalid value, but should be displayed each time they do so.Such a message should only be displayed if the user enters an invalid value, but should be displayed each time they do so.

Our algorithm must be adapted accordingly.Our algorithm must be adapted accordingly.

Page 15: C++loop statements

Algorithm (Algorithm (revisedrevised))

0. Receive 0. Receive MENUMENU, , firstChoicefirstChoice, , lastChoicelastChoice..

1. Loop1. Loop

a. Display a. Display MENUMENU via via coutcout..

b. Read b. Read choicechoice from from cincin..

c. If c. If firstChoicefirstChoice <= <= choicechoice and and choicechoice <= <= lastChoicelastChoice: :

terminate repetition.terminate repetition.

d. Display error message.

End loop.End loop.

2. Return 2. Return choicechoice..

Page 16: C++loop statements

OrganizationOrganization

Our algorithm no longer terminates the Our algorithm no longer terminates the repetition at the repetition at the bottombottom of the loop. of the loop.

Instead, it terminates repetition in the Instead, it terminates repetition in the middle of the loop, suggesting a middle of the loop, suggesting a forever loopforever loop..

Which loop is best used depends on Which loop is best used depends on where execution leaves the loop in where execution leaves the loop in one’s one’s algorithmalgorithm..

Page 17: C++loop statements

CodingCoding

char GetValidMenuChoice(const string MENU,char firstChoice, char lastChoice)

{ char choice; for (;;) { cout << MENU; cin >> choice;

if (choice >= firstChoice && choice <= lastChoice) break;

cerr << “\n*** Invalid menu choice: \’“ << choice << “\’ entered.\n” << endl; }

return choice;}

Page 18: C++loop statements

DiscussionDiscussion

C++ provides a variety of loops:C++ provides a variety of loops:– The The for loopfor loop for counting problems for counting problems

– The The while loopwhile loop for problems where for problems where repetition should be terminated at the repetition should be terminated at the loop’s beginning.loop’s beginning.

– The The do loopdo loop for problems where repetition for problems where repetition should be terminated at the loop’s end.should be terminated at the loop’s end.

– The The forever loopforever loop for problems where for problems where repetition should be terminated at the repetition should be terminated at the loop’s middle.loop’s middle.

Page 19: C++loop statements

Using the FunctionUsing the Function

If a programmer now writes the same If a programmer now writes the same thing:thing:

#include “Menu.h”

int main(){ const string MENU = “Please enter:\n” “ a - to do this\n” “ b - to do that\n” “ c - to do the other\n” “--> “;

char choice = GetValidMenuChoice(MENU, ‘a’, ‘c’);

// ...}

Page 20: C++loop statements

TestingTestingPlease enter: a - to do this b - to do that c - to do the other--> s

** Invalid menu choice ‘s’ entered.

Please enter: a - to do this b - to do that c - to do the other--> a...

Page 21: C++loop statements

SummarySummary

C++ provides C++ provides four different loopsfour different loops for for eliciting repetitive behavior.eliciting repetitive behavior.

(We’ll see the while loop next time.)(We’ll see the while loop next time.)

Which one is best to solve a given Which one is best to solve a given problem depends on the problem depends on the algorithmalgorithm for for that problem.that problem.