Download presentation
Presentation is loading. Please wait.
Published byKory Bradley Modified over 9 years ago
1
Repetition Repeating the Execution of Statements
2
Review We’ve seen that the C++ for loop permits a statement to be executed repeatedly: for (Expr 1 ; Expr 2 ; Expr 3 ) Statement Expr 1 Expr 2 Statement Expr 3 F T
3
A Counting Loop The for loop is most commonly used to count from one value first to another value last: for (int count = first; count <= last; count++) Statement count = first count <= last Statement count++ F T
4
Other Loops C++ also provides the forever loop: a for loop without expressions: for (;;) { StatementList 1 if (Expression) break; StatementList 2 } StatementList 1 Expression F T StatementList 2 Repetition continues so long as Expression is false!
5
Pretest Loops If StatementList 1 is omitted from a forever loop, we get a test-at-the-top or pretest loop: for (;;) { if (Expression) break; StatementList 2 } Expression F T StatementList 2
6
The while Loop For such situations, C++ provides the more readable while loop, whose pattern is: while (Expression) Statement Expression T F Statement Statement can be either a single or compound C++ statement. Repetition continues so long as Expression is true!
7
Post-test Loops If StatementList 2 is omitted in a forever loop, we get a test-at-the-bottom or post-test loop: for (;;) { StatementList 1 if (Expression) break; } Expression FT StatementList 1
8
The do Loop For such situations, C++ provides the more readable do loop, whose pattern is: do Statement while (Expression); Statement can be either a single or compound C++ statement. Repetition continues so long as Expression is true! Expression TF Statement
9
Choosing a Loop With four loops at our disposal, how do we know which one to use? Use the for loop for counting problems.Use the for loop for counting problems. Design algorithms for non-counting problems using a general Loop statement, and see where it is appropriate for repetition to terminate:Design algorithms for non-counting problems using a general Loop statement, and see where it is appropriate for repetition to terminate: –If at the loop’s beginning, use the while loop –If at its end, use the do loop –If in its middle, use the forever loop.
10
Example Write a function that given a positive int value, returns a string of equivalent digits. Example:123 “123” Example:123 “123” 0 “” 0 “”
11
Algorithm 0. Receive intVal. 1. Initialize stringVal to the empty string; 2. Loop a. Set intDigit to the remainder of intVal / 10. b. Compute charDigit, the char equivalent to intDigit. c. Set stringVal to charDigit + stringVal. d. Set intVal to the quotient of intVal / 10. End loop. 3. Return stringVal. Question: How/where should repetition terminate?
12
Choice of Loop Our loop should terminate when intVal <= 0. We should check this condition at the beginning, because if intVal is initially zero (or negative), we do not want any of the statements in the loop’s body to execute. The pretest loop is thus the appropriate choice for this particular problem.
13
Algorithm (revised) 0. Receive intVal. 1. Initialize stringVal to the empty string; 2. Loop a. If (intVal <= 0): terminate repetition. b. Set intDigit to the remainder of intVal / 10. c. Compute charDigit, the char equivalent to intDigit. d. Set stringVal to charDigit + stringVal. e. Set intVal to the quotient of intVal / 10. End loop. 3. Return stringVal.
14
Coding 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; }
15
Discussion The four C++ loops provide very different behaviors: The for loop is a loop designed for counting that provides pretest behavior.The for loop is a loop designed for counting that provides pretest behavior. The while loop is a general-purpose loop that provides test-at-the-top behavior.The while loop is a general-purpose loop that provides test-at-the-top behavior. The do loop is a general-purpose loop that provides test-at-the-bottom behavior.The do loop is a general-purpose loop that provides test-at-the-bottom behavior. The forever loop is a general-purpose loop that provides test-in-the-middle behavior.The forever loop is a general-purpose loop that provides test-in-the-middle behavior.
16
Discussion The while and for loops have their tests at the top, implying that if the loop’s condition is initially false, the body of the loop will not execute, which is called. The while and for loops have their tests at the top, implying that if the loop’s condition is initially false, the body of the loop will not execute, which is called zero-trip behavior. The do loop has its test at the bottom, implying that the body of the loop will execute at least once, regardless of the value of the loop’s condition, which is called. The do loop has its test at the bottom, implying that the body of the loop will execute at least once, regardless of the value of the loop’s condition, which is called one-trip behavior.
17
Discussion The forever loop its test in the middle: –Statements in StatementList 1 will be executed at least once, regardless of the value of Expression. –Statements in StatementList 2 will not be executed if Expression causes repetition to terminate. This might be called. This might be called half-trip behavior.
18
Summary 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- middle loop.The forever loop, a general-purpose test-in-the- middle loop. Which loop you use to solve a given problem should be determined by your algorithm for that problem.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.