while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... ·...

29
while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College Mon, Sep 23, 2019 Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 1 / 25

Transcript of while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... ·...

Page 1: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

while LoopsLecture 12

Sections 5.8 - 5.9

Robb T. Koether

Hampden-Sydney College

Mon, Sep 23, 2019

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 1 / 25

Page 2: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

1 while Loops

2 Input LoopsLoops Controlled by a Sentinel ValueLoops Controlled by End-of-File

3 Assignment

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 2 / 25

Page 3: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Outline

1 while Loops

2 Input LoopsLoops Controlled by a Sentinel ValueLoops Controlled by End-of-File

3 Assignment

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 3 / 25

Page 4: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Iterative Structures

An iterative structure allows a block of statements to be executedrepeatedly.The iteration continues until a specified condition fails, then itterminates.Computers derive their immense computational power through acombination of decision structures, iterative structures, and speed.

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 4 / 25

Page 5: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

The while Statement

The while statement will repeatedly execute a block ofstatements as long as a specified boolean expression is true.Once the boolean expression is false, execution exits the whileloop and continues with the next statement following the loop.

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 5 / 25

Page 6: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

The while Statement

The while Statementwhile (boolean-expression){

action}

Special situationsIf the boolean-expression is initially false, then the action is neverexecuted.If the boolean-expression is always true, then the loop never stops.

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 6 / 25

Page 7: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Examples

ExamplesSumNIntegers.cpp

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 7 / 25

Page 8: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Outline

1 while Loops

2 Input LoopsLoops Controlled by a Sentinel ValueLoops Controlled by End-of-File

3 Assignment

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 8 / 25

Page 9: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Input Loops

Often the purpose of a loop is to process a list of numbers as theyare 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 Mon, Sep 23, 2019 9 / 25

Page 10: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Outline

1 while Loops

2 Input LoopsLoops Controlled by a Sentinel ValueLoops Controlled by End-of-File

3 Assignment

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 10 / 25

Page 11: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Loops Controlled by a Sentinel Value

A sentinel value is a special value appended to the input toindicate 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 Mon, Sep 23, 2019 11 / 25

Page 12: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Unrolling a Loop

The pattern in the loop is1 prompt user for input2 read input3 test for sentinel value4 carry out the action5 prompt6 read7 test8 action9

...

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 12 / 25

Page 13: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

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 whilestatement.Therefore, the first prompt and read must come before the whileloop.And the body of the while loop must follow the pattern:

actionpromptread

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 13 / 25

Page 14: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Unrolling a Loop

promptreadtestactionpromptreadtestaction :

The “unrolled” loop

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 14 / 25

Page 15: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Unrolling a Loop

promptreadtestactionpromptreadtestaction :

repeat

Repeat after the action

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 14 / 25

Page 16: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Unrolling a Loop

promptreadtestactionpromptreadtestaction :

repeat

test

The test is here

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 14 / 25

Page 17: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Unrolling a Loop

promptreadtestactionpromptreadtestaction :

repeattest

The test must be in the top of the loop

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 14 / 25

Page 18: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Unrolling a Loop

promptreadtestactionpromptread

whileloop

This must be the while loop

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 14 / 25

Page 19: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Loops Controlled by a Sentinel Value

Loops Controlled by a Sentinel Valueconst int SENTINEL = value;int number;prompt user for inputcin >> number;while (number != SENTINEL){

actionprompt user for inputcin >> number;

}

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 15 / 25

Page 20: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Loops Controlled by a Sentinel Value

ExampleSentinelSum.cpp

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 16 / 25

Page 21: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Outline

1 while Loops

2 Input LoopsLoops Controlled by a Sentinel ValueLoops Controlled by End-of-File

3 Assignment

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 17 / 25

Page 22: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Detecting End of File (EOF)

There is an istream function eof() (end of file) that returnstrue 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 Mon, Sep 23, 2019 18 / 25

Page 23: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Using the eof() Function

Using the eof() Functionint number;prompt user for inputcin >> number;while (!cin.eof()){

actionprompt user for inputcin >> number;

}

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 19 / 25

Page 24: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Example of EOF

ExampleEOFFuncSum.cpp

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 20 / 25

Page 25: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

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 Mon, Sep 23, 2019 21 / 25

Page 26: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Loops Controlled by EOF

Loops Controlled by EOFint number;prompt user for inputwhile (cin >> number){

actionprompt user for input

}

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 22 / 25

Page 27: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Example of EOF

ExampleEOFOpSum.cpp

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 23 / 25

Page 28: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Outline

1 while Loops

2 Input LoopsLoops Controlled by a Sentinel ValueLoops Controlled by End-of-File

3 Assignment

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 24 / 25

Page 29: while Loops - Lecture 12 Sections 5.8 - 5people.hsc.edu/faculty-staff/robbk/Coms261/Lectures... · while Loops Lecture 12 Sections 5.8 - 5.9 Robb T. Koether Hampden-Sydney College

Assignment

AssignmentRead Sections 5.8 - 5.9.

Robb T. Koether (Hampden-Sydney College) while Loops Mon, Sep 23, 2019 25 / 25