Download sentinel loop

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C syntax wikipedia , lookup

Structured programming wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

For loop wikipedia , lookup

Transcript
9. ITERATIONS AND
LOOP STRUCTURES
Rocky K. C. Chang
October 18, 2015
(Adapted from John Zelle’s slides)
Objectives
• To understand the concepts of definite and indefinite
loops as they are realized in the Python for and while
statements.
• To understand the programming patterns interactive loop
and sentinel loop and their implementations using a
Python while statement.
• To understand the programming pattern end-of-file loop
and ways of implementing such loops in Python.
For Loops: A Quick Review
• The for statement allows us to iterate through a
sequence of values.
• for <var> in <sequence>:
<body>
• The loop index variable var takes on each successive
value in the sequence, and the statements in the body of
the loop are executed once for each value.
Indefinite Loops
• The for loop is a definite loop, meaning that the
number of iterations is determined when the loop
starts.
• We can’t use a definite loop unless we know the
number of iterations ahead of time.
• The indefinite or conditional loop keeps iterating
until certain conditions are met.
Indefinite Loops
• while <condition>:
<body>
• condition is a Boolean expression, just like in
if statements. The body is a sequence of one or
more statements.
• Semantically, the body of the loop executes
repeatedly as long as the condition remains true.
When the condition is false, the loop terminates.
A Pre-test Loop
An example
• Here’s an example of using a while loop to print out
range(10).
i = 0
while i <= 9:
print(i)
i = i + 1
• The code has the same output as this for loop:
for i in range(10):
print i
EXERCISE 9.1
Use a while loop to print out range(2, 10, 2).
EXERCISE 9.2
Try
i = 0
while i <= 9:
print(i)
Interactive Loops
# average2.py
#
A program to average a set of numbers
#
Illustrates interactive loop with two accumulators
moredata = "yes"
sum = 0.0
count = 0
while moredata[0] == 'y':
x = int(input("Enter a number >> "))
sum = sum + x
count = count + 1
moredata = input("Do you have more numbers (yes or
no)? ")
print ("\nThe average of the numbers is", sum / count)
EXERCISE 9.3
One problem with the program is that if you
type too fast, you may enter a number as an
answer to the yes/no question. What will the
program respond to this "error"? How will you
modify the program to rectify the response?
Sentinel Loops
• A sentinel loop continues to process data until reaching a
special value that signals the end.
• This special value is called the sentinel.
• The sentinel must be distinguishable from the data since it
is not processed as part of the data.
get the first data item
while item is not the sentinel
process the item
get the next data item
EXERCISE 9.4
Apply the sentinel approach to implementing
the program for returning the average on slide
10.
File Loops
# average5.py
#
Computes the average of numbers listed in a file.
def main():
fileName = input("What file are the numbers in? ")
infile = open(fileName,'r')
sum = 0.0
count = 0
for line in infile:
sum = sum + eval(line)
count = count + 1
print ("\nThe average of the numbers is", sum / count)
EXERCISE 9.5
Instead of using a list of lines, you could also
the readline() method to read one line at a
time. In this case, you need to detect the endof-file yourself. The sentinel for the end of file
is line == "". Try to implement it.
Nested Loops
• Suppose we change our specification to allow any
number of numbers on a line in the file (separated by
commas), rather than one per line.
sum = 0.0
count = 0
for line in infile:
# update sum and count for values in line
# Since we have multiple items to process,
# we can use another loop for this.
print ("\nThe average of the numbers is", sum
/ count)
EXERCISE 9.6
Implement the remaining codes for the
program in the previous slide.
Designing Nested Loops
• Break the problem into two parts.
• The first is to process line by line (outer loop).
• The second is to process the items on each line (inner
loop).
• Note that the two parts are not inter-dependent.
Post-Test Loop
• Say we want to write a program that is supposed to get a
nonnegative number from the user.
• If the user types an incorrect input, the program asks for
another value.
• This process continues until a valid value has been
entered.
• This process is input validation.
Post-Test Loop
repeat
get a number from the user
until number is >= 0
Post-Test Loop
• Python doesn’t have a built-in statement to do this, but we
can do it with a slightly modified while loop.
• We could simulate a post-test loop by
number = -1
while number < 0:
number = eval(input("Enter a positive number:
"))
Using a break
• Some programmers prefer to simulate a post-test loop by
using the Python break statement.
• Executing break causes Python to immediately exit the
enclosing loop.
• For example,
while True:
number = eval(input("Enter a positive number:
"))
if number >= 0:
break # Exit loop if number is valid
• Avoid using break often within loops, because the logic of
a loop is hard to follow when there are multiple exits.
EXERCISE 9.7
Enhance the codes in the previous page by
printing out an error message if the input is
not valid.
Loop and a Half
while True:
get next data item
if the item is the sentinel:
break
process the item
END