Iterative Structures: for Loops - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Iterative Structures: for Loops

Description:

If we know in advance the number of times a loop should be executed, then we can ... This is trickier, as we discover when we write the sequence of steps. 10/5/09 ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 18
Provided by: peopl93
Category:

less

Transcript and Presenter's Notes

Title: Iterative Structures: for Loops


1
Iterative Structures for Loops
  • Lecture 13 Wed, Sep 24, 2003

2
Loops Controlled by a Counter
  • If we know in advance the number of times a loop
    should be executed, then we can count the
    iterations and quit at the proper time.
  • Establish a counter and do the following.
  • Initialize the counter to 0.
  • Test the counter on each iteration.
  • Increment the counter.

3
Loops Controlled by a Counter
  • If the counter controls the loop, then the
    testing does not involve the input.
  • Therefore, the pattern prompt-read-test-action is
    no longer in effect.
  • Indeed, there may not be any input.

4
Loops Controlled by a Counter
int limit cin gtgt limit int counter 0 while
(counter lt limit) Prompt user for input
cin gtgt number Action counter
5
Example of a Counter
  • CounterSum.cpp

6
Combining the Methods
  • A loop may be controlled by a combination of
    methods.
  • For example, we may use a sentinel value of 1,
    but also test for EOF.
  • This loop would be controlled by both a sentinel
    value and end-of-file.

7
Combining the Methods
  • The sequence should be
  • Prompt user for input
  • Read input
  • Test input and test for EOF
  • Action
  • Prompt user for input
  • Read input
  • Test input and test for EOF
  • Action
  • And so on

8
Combining the Methods
const int SENTINEL -1 Prompt user for
input cin gtgt number while (!cin.eof() number
! SENTINEL) Action Prompt user for
input cin gtgt number
9
Example of a Sentinel Value and EOF
  • SentinelEOFSum.cpp

10
Combining the Methods
  • As another example, we may use a sentinel value
    of 1, but also not read more than 100 numbers.
  • This loop would be controlled by both a sentinel
    value and a counter.
  • This is trickier, as we discover when we write
    the sequence of steps.

11
Combining the Methods
  • The sequence should be
  • Initialize counter
  • Test counter
  • Prompt user for input
  • Read input
  • Test input
  • Action
  • Increment counter
  • Test counter
  • And so on

12
Combining the Methods
  • This loop requires two tests and they must occur
    at different points.
  • This is one of the rare cases where it is
    justifiable to break out of a loop prematurely.
  • Place one test in the while statement.
  • Place the other test in the body of the loop
    together with a break statement.

13
Combining the Methods
const int LIMIT 100 const int SENTINEL
-1 int counter 0 while (counter lt limit)
Prompt user for input cin gtgt number if
(number SENTINEL) break Action
counter
14
Example of a Sentinel Value and a Counter
  • SentinelCounterSum.cpp

15
The for Statement
  • Loops controlled by counters are very common.
  • To facilitate their use, C provides a for
    statement.
  • The for statement controls the loop by
  • Initializing the counter
  • Testing the counter
  • Incrementing the counter

16
The for Statement
for (Init counter Test counter Incr counter)
Action
for (int i 0 i lt 10 i) cout ltlt "Enter
a number " cin gtgt number sum
number
17
Examples of a for Loop
  • ForSum.cpp
  • CountLetters.cpp
Write a Comment
User Comments (0)
About PowerShow.com