C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.

Slides:



Advertisements
Similar presentations
Parameter Passing Mechanisms Reference Parameters.
Advertisements

File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Loops and Files.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Software Engineering 1 (Chap. 1) Object-Centered Design.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
1 Programming and Problem Solving — Software Engineering (Read Chap. 2)
Programming and Problem Solving — Software Engineering (Read Chap. 2) 1.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
1 C++ Loop Statements Repetition Revisited. 2 Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
Controlling Function Behavior Sequence, Selection and Repetition.
File I/O ifstreams and ofstreams Sections 11.1 &
Parameter Passing Mechanisms Reference Parameters Read § §
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
C++ An Introduction to Computing, 3rd ed. 1 Repetition Chapter 7.
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Parameter Passing Mechanisms Reference Parameters § §
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
Overview Go over parts of quiz? Another iteration structure for loop.
Repetition Repeating the Execution of Statements.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
1 CS161 Introduction to Computer Science Topic #8.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Calvin College Controlling Behavior The if, switch and for Statements.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Controlling Behavior The if and for Statements.
ifstreams and ofstreams
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Repeating the Execution of Statements
CS161 Introduction to Computer Science
Functions Manipulating Files
More Repetition Chap. 8 (Read § ) 1.
Writing Reuseable Formulas
Programming Fundamentals
solve the following problem...
Chapter 8 Repetition Statements
Control Structures Part 1
Looping III (do … while statement)
Fundamental Programming
ifstreams and ofstreams
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Controlling Behavior The if and for Statements.
Presentation transcript:

C++ Loop Statements Repetition Revisited

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

Preliminary Analysis The tricky part is that the function must return a value that (i) is a valid menu choice; and (ii) was chosen by the user. One way to accomplish both goals is to use a loop that displays the menu, reads the user’s choice, checks its validity, and if it is not valid, gives them another chance...

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

Objects Description 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

Operations Description 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

Algorithm 0. Receive MENU, firstChoice, lastChoice. 1. Loop a. Display MENU via cout. b. Read choice from cin. c. If firstChoice <= choice and choice <= lastChoice: terminate repetition. End loop. End loop. 2. Return choice.

Organization Note that our algorithm terminates the repetition at the bottom of the loop. To code such loops conveniently and readably, C++ provides the do loop.

Coding char GetValidMenuChoice(const string MENU, char firstChoice, char lastChoice) { char choice; do { cout << MENU; cin >> choice; } while (choice lastChoice); return choice; }

Discussion The do loop tests its condition at the end of the loop, making it useful for any problem in which the body of the loop must be performed at least once. This function seems general enough to be reuseable by any menu-using program, so it should be stored in a library.

Using the Function Once our function is stored in a library, a 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’); //... }

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

Note 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. Our algorithm must be adapted accordingly.

Algorithm (revised) 0. Receive MENU, firstChoice, lastChoice. 1. Loop a. Display MENU via cout. b. Read choice from cin. c. If firstChoice <= choice and choice <= lastChoice: terminate repetition. d. Display error message. End loop. End loop. 2. Return choice.

Organization Our algorithm no longer terminates the repetition at the bottom of the loop. Instead, it terminates repetition in the middle of the loop, suggesting a forever loop. Which loop is best used depends on where execution leaves the loop in one’s algorithm.

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

Discussion C++ provides a variety of loops: –The for loop for counting problems –The while loop for problems where repetition should be terminated at the loop’s beginning. –The do loop for problems where repetition should be terminated at the loop’s end. –The forever loop for problems where repetition should be terminated at the loop’s middle.

Using the Function If a programmer now writes the same 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’); //... }

Testing Please 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...

Summary C++ provides four different loops for eliciting repetitive behavior. (We’ll see the while loop next time.) Which one is best to solve a given problem depends on the algorithm for that problem.