COMP 110 Loops, loops, loops, loops, loops, loops - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 Loops, loops, loops, loops, loops, loops

Description:

Ending a loop. Count-controlled loops. If you know the number of loop iterations ... user enters a negative number (a sentinel value), it's time to end the loop ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 32
Provided by: csU65
Learn more at: http://www.cs.unc.edu
Category:
Tags: comp | ending | loops

less

Transcript and Presenter's Notes

Title: COMP 110 Loops, loops, loops, loops, loops, loops


1
COMP 110Loops, loops, loops, loops, loops, loops
  • Luv Kohli
  • September 22, 2008
  • MWF 2-250 pm
  • Sitterson 014

2
Announcements
  • Program 2 due date extended to Friday, Sep 26,
    2pm
  • Lab 4 due date extended to Wednesday, Oct 1, 2pm
  • Print out your programs and hand the in
  • Interesting talk today at 4pm, SN011
  • William Swartout, Toward the Holodeck
    Integrating Graphics, Artificial Intelligence,
    Entertainment and Learning

3
Questions?
  • How is Program 2 going?

4
Today in COMP 110
  • More loop examples
  • If time indentation

5
Designing a loop
  • Initializing statements
  • Loop body
  • Ending a loop

6
Designing a loop body
  • Example calculate the sum of numbers entered by
    the user

7
Identify the loop body from pseudocode
  • Output instructions to the user
  • Initialize variables
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • ...
  • Output the sum

Repeated statementsbecome your loop body
Statements that are only done onceare not part
of your loop body
8
Pseudocode with loop body
  • Output instructions to the user
  • Initialize variables
  • Do the following for the appropriate number of
    times
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • Output the sum

Initializing statements
How do we end the loop?How many iterations?
9
Initializing statements
  • Variables used in your loop need to be
    initialized (set to a value) before the loop
  • next
  • Read a number into variable next
  • We read a new value for next before using it
    during each iteration of the loop so we do not
    need to initialize it
  • sum
  • sum sum next
  • sum is on the right side of an assignment
    statement. sum MUST have a valid value before the
    loop starts.

10
What should sums initial value be?
  • Output instructions to the user
  • Initialize variables
  • Do the following for the appropriate number of
    times
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • Output the sum

11
Initialize sum
  • Consider the first iteration.
  • After executing the first iteration, the expected
    value of sum should be
  • sum next (the first input value)
  • The assignment statement is
  • sum sum next
  • Therefore, initial value of sum is
  • sum 0

12
What should sums initial value be?
  • Output instructions to the user
  • sum 0
  • Do the following for the appropriate number of
    times
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • Output the sum

Initializing statements
13
Initializing statements
  • Always set initial value to 0?
  • What if we calculate the product of all the input
    values?
  • product product next
  • After the first iteration, we expect
  • product next (the first input value)
  • Initial value of product?
  • product 1

14
Ending a loop
  • Output instructions to the user
  • sum 0
  • Do the following for the appropriate number of
    times
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • Output the sum

How do we end the loop?How many iterations?
15
Ending a loop
  • Count-controlled loops
  • If you know the number of loop iterations
  • for (count 0 count lt iterations count)
  • User-controlled loops
  • Ask-before-iterating
  • Sentinel value

16
Count-controlled loops
  • You know how many iterations in advance.
  • Ask every student in the class for his/her age
    and calculate the average age
  • sum 0
  • for (student 1 to student n)
  • Ask his/her age
  • sum sum age
  • average sum / n

17
Count-controlled loops
  • Use a for loop in most cases
  • 37 students in the class 37 iterations
  • numStudents 37
  • sum 0
  • for (count 1 count lt numStudents count)
  • Ask student count for his/her age
  • sum sum age
  • average sum / numStudents

18
Count-controlled loops
  • Can also use a while loop, but for loop is
    easiest way to implement count-controlled loops
  • numStudents 37
  • count 1
  • sum 0
  • while (count lt numStudents)
  • Ask student count for his/her age
  • sum sum age
  • count count 1
  • average sum / numStudents

19
User-controlled loops
  • Also called ask-before-iterating
  • Ask the user if it is time to end the loop
  • Example add a bunch of numbers
  • Lab 4 when the user enters a negative number (a
    sentinel value), its time to end the loop

20
User-controlled loops
  • Use while and do-while in most cases
  • do-while
  • If you know that the user wants at least one
    iteration
  • Ask the user whether the loop should end at the
    end of the loop body

21
User-controlled loops do-while
  • do
  • // do stuff in your code here
  • Prompt user for input
  • Read a number into variable next
  • sum sum next
  • // ask if we should do another iteration
  • System.out.print(Continue (yes/no)? )
  • answer keyboard.nextLine()
  • while (answer.equalsIgnoreCase(yes))

22
User-controlled loops while
  • Use while if there could be zero iterations
  • Example, summing numbers
  • When the user inputs a negative value
  • End the loop
  • The negative value is not used in calculating the
    sum
  • If the first input value is negative,no
    iterations

23
User-controlled loops while
  • sum 0
  • while (next gt 0)
  • Prompt user for input
  • Read a number into variable next
  • sum sum next

Is this code correct?
What is the initial value of next?
24
Solution
  • sum 0
  • Prompt user for input
  • Read a number into variable next
  • while (next gt 0)
  • Prompt user for input
  • Read a number into variable next
  • sum sum next

Is this code correct?
25
Solution, another try
  • sum 0
  • Prompt user for input
  • Read a number into variable next
  • while (next gt 0)
  • sum sum next
  • Prompt user for input
  • Read a number into variable next

Is this code correct?
26
Example problem
  • Find the lowest and highest ages in the class
  • Use a loop?
  • Yes
  • What goes in the loop body?

27
Loop body
  • Ask student 1 for age
  • Update min/max ages
  • Ask student 2 for age
  • Update min/max ages
  • ...
  • ...
  • Ask student 37 for age
  • Update min/max ages
  • End loop
  • Output min and max ages

28
Min/max ages
  • Which kind of loop?
  • Lets use a for loop

29
Min/max ages
  • int min 2000 // initialize to large value
  • int max 0 // initialize to small value
  • for (int count 1 count lt 37 count)
  • Ask student count for age
  • if (age gt max)
  • max age
  • if (age lt min)
  • min age
  • Output min and max ages

30
Min/max ages
min
max
Ages
2000 20 20 18 18 18 12 12 12
0 20 23 23 23 25 25 94 94
20 23 18 18 25 12 94 36
if (age gt max) max age if (age lt min)
min age
31
Wednesday
  • Nested loops
  • Loop bugs
Write a Comment
User Comments (0)
About PowerShow.com