Programming with Loops - PowerPoint PPT Presentation

About This Presentation
Title:

Programming with Loops

Description:

Programming with Loops When to Use a Loop Whenever you have a repeated set of actions, you should consider using a loop. For example, if you have to read in the exam ... – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 13
Provided by: Robe113
Learn more at: https://www.ecs.csun.edu
Category:

less

Transcript and Presenter's Notes

Title: Programming with Loops


1
Programming with Loops
2
When to Use a Loop
  • Whenever you have a repeated set of actions, you
    should consider using a loop.
  • For example, if you have to read in the exam
    scores for all students in the class, you should
    write code to read in just one score and repeat
    it for all students in the class.
  • If we needed the sum of all the scores we could
    add each new score as it was entered to a
    variable containing the sum.

3
Initializing Statements
  • An initializing statement just sets a beginning
    or initial value for a variable to be used later.
  • If we dont initialize a variable before it is
    used, we cant be sure what value it contains.
  • For example, in order to be able to sum values
    into a variable, that variable must be
    initialized to zero before we begin.

4
Initializing Statements (contd)
  • You dont always initialize variables to zero.
  • For example,
  • for (count 1, count lt n count)
  • Read a number into the variable next.
  • product product next

5
Ending a Loop
  • If you know the number of iterations required
    before entering the loop you can use a for loop.
    Such loops are called count-controlled loops.
  • One way to end the loop is to ask the user if
    its time to end the loop. This is called
    ask-before-iterating.
  • A third way to end a loop is by using a sentinel
    value.

6
Ending a Loop Using a Sentinel Value
  • A sentinel value is a special input value,
    different from valid input values, that is used
    to signal the end of the loop.
  • For example, if you are reading in exam scores
    and you dont know how many you have, if you know
    that no score can be less that zero, you can use
    a negative value as the sentinel.
  • Your condition can just say to keep reading
    scores until you read a negative score. Then
    youre done.

7
Example of Using a Sentinel Value to End a Loop
  • System.out.println(Enter scores for all
    students.)
  • System.out.println(Enter a negative number
    after)
  • System.out.println(you have entered the
    scores.)
  • Scanner keyboard new Scanner(System.in)
  • double max keyboard.nextDouble()
  • double min max//The max and min so far are the
    first score.
  • double next keyboard.nextDouble()
  • while (next gt 0)
  • if (next gt max)
  • max next
  • if (next lt min)
  • min next
  • next keyboard.nextDouble()
  • System.out.println(The highest score is
    max)
  • System.out.println(The lowest score is min)

8
Nested Loop Example
  • do
  • System.out.println()
  • System.out.println(Enter all the scores to be
    averaged.)
  • System.out.println(Enter a negative number
    after)
  • System.out.println(you have entered all the
    scores.)
  • sum 0
  • numberOfStudents 0
  • next keyboard.nextDouble()
  • while (next gt 0)
  • sum sum next
  • numberOfStudents
  • next keyboard.nextDouble()
  • if (numberOfStudents gt 0)
  • System.out.println(The average is
    (sum/numberOfStudents))
  • else
  • System.out.println(No scores to average.)

9
Loop Bugs
  • Programs with loops are more likely to have bugs
    than simpler programs.
  • The two most common bugs associated with loops
    are
  • Unintended infinite loops
  • Off-by-one errors

10
Unintended Infinite Loops
  • If the condition tested to end the loop is
    incorrectly written, e.g. (a lt 0) instead of (a gt
    0), the loop may never end if a starts off with a
    positive value and during each iteration a is
    increased.
  • Be aware that a loop may work correctly for some
    input values, but become an infinite loop for
    others. Running one test case may not be enough
    to insure correctness.

11
Off-By-One Bugs
  • These errors are the ones where your loop
    iterates one to many or one to few times. You
    may have written the condition as (number lt 0)
    when you really meant (number lt 0).
  • To reduce the possibility of these errors you
    should always test at and near the values used in
    the condition controlling the loop.

12
Tracing Variables
  • If your program does not work correctly and it is
    not clear what the problem is, it is probably a
    good idea to trace some key variables. That is
    to watch what happens to them as they are changed
    when the program executes.
  • Many systems have built in utilities for tracing
    variables. If not you can trace them by printing
    them out from the program, or you can trace your
    algorithm by hand using pencil and paper.
Write a Comment
User Comments (0)
About PowerShow.com