repetition repeating the execution of statements

18
Repetition Repetition Repeating the Execution of Repeating the Execution of Statements Statements

Upload: kory-bradley

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Repetition Repeating the Execution of Statements

RepetitionRepetition

Repeating the Execution of StatementsRepeating the Execution of Statements

Page 2: Repetition Repeating the Execution of Statements

ReviewReview

We’ve seen that the C++ We’ve seen that the C++ for for looploop permits a statement permits a statement to be executed repeatedly:to be executed repeatedly:

for (Expr1; Expr2; Expr3)Statement

Expr1

Expr2

Statement

Expr3

F

T

Page 3: Repetition Repeating the Execution of Statements

A Counting LoopA Counting Loop

The for loop is most commonly The for loop is most commonly used to count from one value used to count from one value firstfirst to another value to another value lastlast::

for (int count = first; count <= last; count++)

Statement

count = first

count <= last

Statement

count++

F

T

Page 4: Repetition Repeating the Execution of Statements

Other LoopsOther Loops

C++ also provides the forever C++ also provides the forever loop: a for loop without loop: a for loop without expressions:expressions:

for (;;){StatementList1

if (Expression) break; StatementList2}

StatementList1

Expression

F

T

StatementList2

Repetition continues so long Repetition continues so long as as ExpressionExpression is is falsefalse!!

Page 5: Repetition Repeating the Execution of Statements

Pretest LoopsPretest Loops

If If StatementListStatementList11 is omitted from a forever is omitted from a forever loop, we get a loop, we get a test-at-the-top test-at-the-top oror pretest pretest loop:loop:

for (;;){ if (Expression) break; StatementList2}

Expression

F

T

StatementList2

Page 6: Repetition Repeating the Execution of Statements

The while LoopThe while LoopFor such situations, C++ provides the more For such situations, C++ provides the more

readable readable while loopwhile loop, whose pattern is:, whose pattern is:

while (Expression) Statement Expression

T

F

StatementStatementStatement can be either a can be either a

single or compound C++ single or compound C++ statement.statement.

Repetition continues so Repetition continues so long as long as ExpressionExpression is is truetrue!!

Page 7: Repetition Repeating the Execution of Statements

Post-test LoopsPost-test Loops

If If StatementListStatementList22 is omitted in a forever loop, is omitted in a forever loop, we get a we get a test-at-the-bottom test-at-the-bottom oror post-test post-test loop:loop:

for (;;){ StatementList1 if (Expression) break;}

Expression FT

StatementList1

Page 8: Repetition Repeating the Execution of Statements

The do LoopThe do LoopFor such situations, C++ provides the For such situations, C++ provides the

more readable more readable do loopdo loop, whose pattern is:, whose pattern is:

do Statementwhile (Expression);

StatementStatement can be either a can be either a single or compound C++ single or compound C++ statement.statement.

Repetition continues so Repetition continues so long as long as ExpressionExpression is is truetrue!!

Expression TF

Statement

Page 9: Repetition Repeating the Execution of Statements

Choosing a LoopChoosing a Loop

With four loops at our disposal, how do we With four loops at our disposal, how do we know which one to use?know which one to use?

• Use the Use the forfor loop for counting problems. loop for counting problems.

• Design algorithms for non-counting problems Design algorithms for non-counting problems using a general Loop statement, and see using a general Loop statement, and see where it is appropriate for repetition to where it is appropriate for repetition to terminate:terminate:– If at the loop’s beginning, use the If at the loop’s beginning, use the whilewhile loop loop

– If at its end, use the If at its end, use the dodo loop loop

– If in its middle, use the If in its middle, use the foreverforever loop. loop.

Page 10: Repetition Repeating the Execution of Statements

ExampleExampleWrite a function that given a Write a function that given a positivepositive int value, returns a string of equivalent digits. int value, returns a string of equivalent digits.

Example:Example: 123 123 “123”“123”

0 0 “”“”

Page 11: Repetition Repeating the Execution of Statements

AlgorithmAlgorithm

0. Receive 0. Receive intValintVal..

1. Initialize 1. Initialize stringValstringVal to the empty string; to the empty string;

2. Loop2. Loopa. Set a. Set intDigitintDigit to the remainder of to the remainder of intValintVal / 10. / 10.b. Compute b. Compute charDigitcharDigit, the char equivalent to , the char equivalent to intDigitintDigit. . c. Set c. Set stringValstringVal to to charDigitcharDigit + + stringValstringVal..d. Set d. Set intValintVal to the quotient of to the quotient of intValintVal / 10. / 10.End loop.End loop.

3. Return 3. Return stringValstringVal..

Question: How/where should repetition terminate?Question: How/where should repetition terminate?

Page 12: Repetition Repeating the Execution of Statements

Choice of LoopChoice of LoopOur loop should terminate when Our loop should terminate when intValintVal <= <= 0.0.

We should check this condition at the We should check this condition at the beginning, because if beginning, because if intValintVal is initially zero is initially zero (or negative), we do not want any of the (or negative), we do not want any of the statements in the loop’s body to execute.statements in the loop’s body to execute.

The The pretestpretest loop is thus the appropriate loop is thus the appropriate choice for this particular problem.choice for this particular problem.

Page 13: Repetition Repeating the Execution of Statements

Algorithm (Algorithm (revisedrevised))0. Receive 0. Receive intValintVal..

1. Initialize 1. Initialize stringValstringVal to the empty string; to the empty string;

2. Loop2. Loopa. If (intVal <= 0): terminate repetition.b. Set b. Set intDigitintDigit to the remainder of to the remainder of intValintVal / 10. / 10.c. Compute c. Compute charDigitcharDigit, the char equivalent to , the char equivalent to intDigitintDigit. . d. Set d. Set stringValstringVal to to charDigitcharDigit + + stringValstringVal..e. Set e. Set intValintVal to the quotient of to the quotient of intValintVal / 10. / 10.End loop.End loop.

3. Return 3. Return stringValstringVal..

Page 14: Repetition Repeating the Execution of Statements

CodingCoding

We thus choose the while loop for this problem:We thus choose the while loop for this problem:

string IntToString(int intVal){ const int ASCII_ZERO = 48; string stringVal = “”; int intDigit; char charDigit;

while (intVal > 0) { intDigit = intVal % 10; charDigit = char(intDigit + ASCII_ZERO); stringVal = charDigit + stringVal; intVal /= 10; }

return stringVal;}

Page 15: Repetition Repeating the Execution of Statements

DiscussionDiscussion

The four C++ loops provide very different The four C++ loops provide very different behaviors:behaviors:

• The The for loopfor loop is a loop designed for counting that is a loop designed for counting that provides pretest behavior.provides pretest behavior.

• The while loop is a general-purpose loop that The while loop is a general-purpose loop that provides test-at-the-top behavior.provides test-at-the-top behavior.

• The do loop is a general-purpose loop that The do loop is a general-purpose loop that provides test-at-the-bottom behavior.provides test-at-the-bottom behavior.

• The forever loop is a general-purpose loop that The forever loop is a general-purpose loop that provides test-in-the-middle behavior.provides test-in-the-middle behavior.

Page 16: Repetition Repeating the Execution of Statements

DiscussionDiscussion

The The whilewhile and and forfor loops have their tests at the loops have their tests at the top, implying that top, implying that if the loop’s condition is if the loop’s condition is initially false, the body of the loop will not initially false, the body of the loop will not executeexecute, which is called , which is called zero-trip behavior..

The The dodo loop has its test at the bottom, implying loop has its test at the bottom, implying that that the body of the loop will execute at the body of the loop will execute at least once, regardless of the value of the least once, regardless of the value of the loop’s conditionloop’s condition, which is called , which is called one-trip behavior. .

Page 17: Repetition Repeating the Execution of Statements

DiscussionDiscussion

The forever loop its test in the middle: The forever loop its test in the middle:

– Statements in Statements in StatementListStatementList11 will be will be executed executed at least onceat least once, regardless of the , regardless of the value of value of ExpressionExpression..

– Statements in Statements in StatementListStatementList22 will will not be not be executedexecuted if if ExpressionExpression causes repetition to causes repetition to terminate.terminate.

This might be called This might be called half-trip behavior..

Page 18: Repetition Repeating the Execution of Statements

SummarySummary

C++ provides four repetitive execution statements:C++ provides four repetitive execution statements:

• The for loop, for counting.The for loop, for counting.

• The while loop, a general-purpose pretest loop.The while loop, a general-purpose pretest loop.

• The do loop, a general-purpose post-test loop.The do loop, a general-purpose post-test loop.

• The forever loop, a general-purpose test-in-the-The forever loop, a general-purpose test-in-the-middle loop.middle loop.

Which loop you use to solve a given problem Which loop you use to solve a given problem should be determined by your should be determined by your algorithmalgorithm for that for that problem.problem.