while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Similar documents
Minimal Spanning Trees

The Critical-Path Algorithm

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

The Decreasing-Time Algorithm

Solving Recursive Sequences by Iteration

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

The Pairwise-Comparison Method

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

LR Parsing - Conflicts

Implementing Linked Lists

Scope and Parameter Passing

Recursive Descent Parsers

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Scheduling and Digraphs

Density Curves Sections

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Binary Tree Applications

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Scope and Parameter Passing

The Traveling Salesman Problem Brute Force Method

Programming Languages

The Normal Distribution

Friends and Unary Operators

Nondeterministic Programming in C++

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

The Plurality-with-Elimination Method

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

The Graphics Pipeline

Recursive Linked Lists

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Class Construct Part 1

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Stacks and their Applications

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

XQuery FLOWR Expressions Lecture 35

Inheritance: The Fundamental Functions

Magnification and Minification

Street-Routing Problems

List Iterator Implementation

Building the Abstract Syntax Trees

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Programming Languages

The Coefficient of Determination

Introduction to Compiler Design

Displaying Distributions - Quantitative Variables

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Dynamic Allocation of Memory

Dynamic Allocation of Memory

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Function Definition Syntax Tree

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

PHP Queries and HTML Forms Lecture 23

The Graphics Pipeline

Recognition of Tokens

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

The CYK Parsing Algorithm

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Traveling Salesman Problem Cheapest-Link Algorithm

Inheritance: The Fundamental Functions

The Class Construct Part 2

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Fundamental Data Types

Binary Tree Implementation

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Introduction to Databases

The Projection Matrix

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Binary Tree Implementation

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Ambient and Diffuse Light

MySQL Creating a Database Lecture 3

The Standard Template Library Classes

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

The x86 Instruction Set

Insertions, Deletions, and Updates

DTDs and XML Attributes

Transcription:

while Loops Lecture 13 Sections 5.8-5.9 Robb T. Koether Hampden-Sydney College Wed, Sep 26, 2018 Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 1 / 25

1 while Loops 2 Input Loops Loops Controlled by a Sentinel Value Loops Controlled by End-of-File 3 Assignment Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 2 / 25

Outline 1 while Loops 2 Input Loops Loops Controlled by a Sentinel Value Loops Controlled by End-of-File 3 Assignment Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 3 / 25

Iterative Structures An iterative structure allows a block of statements to be executed repeatedly. The iteration continues until a specified condition fails, then it terminates. Computers derive their immense computational power through a combination of decision structures, iterative structures, and speed. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 4 / 25

The while Statement The while statement will repeatedly execute a block of statements as long as a specified boolean expression is true. Once the boolean expression is false, execution exits the while loop and continues with the next statement following the loop. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 5 / 25

The while Statement The while Statement while (boolean-expression) { action } Special situations If the boolean-expression is initially false, then the action is never executed. If the boolean-expression is always true, then the loop never stops. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 6 / 25

Examples Examples SumNIntegers.cpp Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 7 / 25

Outline 1 while Loops 2 Input Loops Loops Controlled by a Sentinel Value Loops Controlled by End-of-File 3 Assignment Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 8 / 25

Input Loops Often the purpose of a loop is to process a list of numbers as they are read in. There are three standard ways to control such a loop. By sentinel value. By end-of-file. By a counter. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 9 / 25

Outline 1 while Loops 2 Input Loops Loops Controlled by a Sentinel Value Loops Controlled by End-of-File 3 Assignment Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 10 / 25

Loops Controlled by a Sentinel Value A sentinel value is a special value appended to the input to indicate the end of the list. For example, if the data represent test scores, a sentinel value of 1 or 999 may be used. Caution The sentinel value must be a value that cannot occur otherwise. The sentinel value should not be processed as regular data. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 11 / 25

Unrolling a Loop The pattern in the loop is 1 prompt user for input 2 read input 3 test for sentinel value 4 carry out the action 5 prompt 6 read 7 test 8 action 9. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 12 / 25

Unrolling a Loop The pattern begins to repeat with the prompt statement. However, the test must occur at the top of the loop, in the while statement. Therefore, the first prompt and read must come before the while loop. And the body of the while loop must follow the pattern: action prompt read Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 13 / 25

Unrolling a Loop prompt read test action prompt read test action : The unrolled loop Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 14 / 25

Unrolling a Loop prompt read test action prompt read test action : repeat Repeat after the action Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 14 / 25

Unrolling a Loop test prompt read test action prompt read test action : repeat The test is here Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 14 / 25

Unrolling a Loop test prompt read test action prompt read test action : repeat The test must be in the top of the loop Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 14 / 25

Unrolling a Loop prompt read test action prompt read while loop This must be the while loop Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 14 / 25

Loops Controlled by a Sentinel Value Loops Controlled by a Sentinel Value const int SENTINEL = value; int number; prompt user for input cin >> number; while (number!= SENTINEL) { action prompt user for input cin >> number; } Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 15 / 25

Loops Controlled by a Sentinel Value Example SentinelSum.cpp Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 16 / 25

Outline 1 while Loops 2 Input Loops Loops Controlled by a Sentinel Value Loops Controlled by End-of-File 3 Assignment Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 17 / 25

Detecting End of File (EOF) There is an istream function eof() (end of file) that returns true when the program attempts to read past the end of a file. Otherwise, it returns false. The while loop may use the boolean expression!cin.eof() When input is through the keyboard, there is no file. In this case, EOF can be simulated by typing CTRL-Z (Windows). Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 18 / 25

Using the eof() Function Using the eof() Function int number; prompt user for input cin >> number; while (!cin.eof()) { action prompt user for input cin >> number; } Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 19 / 25

Example of EOF Example EOFFuncSum.cpp Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 20 / 25

Loops Controlled by EOF When the input operator >> attempts to read past the end of a file, it returns false (0). Otherwise, it returns true (nonzero). Thus, the expression cin >> number may be used as a boolean expression in a while statement. This expression will both read and test the input. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 21 / 25

Loops Controlled by EOF Loops Controlled by EOF int number; prompt user for input while (cin >> number) { action prompt user for input } Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 22 / 25

Example of EOF Example EOFOpSum.cpp Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 23 / 25

Outline 1 while Loops 2 Input Loops Loops Controlled by a Sentinel Value Loops Controlled by End-of-File 3 Assignment Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 24 / 25

Assignment Assignment Read Sections 5.8-5.9. Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 25 / 25