Title: Asserting Java, Ch 7: Repetition
1Asserting Java Rick Mercer
Chapter 7 Repetition
2Chapter 7 Repetition
- Goals
- Recognize the Determinate loop pattern and use
Java's while loop to implement it - Recognize and use the indeterminate loop pattern
and use Java's while loop to implement it - Solve problems that require doing something over
and over again - either a pre-specified number of times,or
- until something happens to indicate you're done
37.1 Repetitive Control
- The following algorithms involve repetition
- Add the remaining flour ¼ cup at a time whipping
until smooth. - While there are more burger/fries/soda orders,
sum each item. Apply tax. Then display the
total - Compute a course grade for every student.
- While the ATM is running, process another
customer, and for each customer allow many
transactions. - Microwave the food until the timer reaches 0, the
cancel button is hit, or the door is opened.
4Why is repetition needed?
- Why Repetition?
- To take advantage of the computer's speed in
order to perform the same tasks faster. - To avoid writing the same statements over and
over again (shorter programs). - To examine all the objects in a collection of
objects. - To make programs general enough to handle various
sized collections of data. - Consider code intended to average exactly 100
numbers (next slide)
5Average 100 values the hard way
How many statements are required for 100
inputs____? What changes are necessary to average
200 inputs ____?
- double sum 0.0
- double average, number
- System.out.print("Enter number ") // lt-
Repeat - number keyboard.nextDouble( ) // lt- these
- sum sum number // lt-
statements - System.out.print("Enter number ") // lt-
Repeat - number keyboard.nextDouble( ) // lt- these
- sum sum number // lt-
statements - // ... many statements not shown here ...
- average sum / 100.0
- System.out.println( "average " average )
6Algorithmic Pattern The Determinate loop
- There is a better way
- We often need to perform some action a specific
number of times - Produce 89 paychecks
- Count down to 0 (take 1 second of the clock).
- Send grade reports to 36,531 UofA students
- The determinate loop pattern repeats some action
a specific number of times
7(No Transcript)
8The while loop
- The indeterminate loop pattern can be
imple-mented with the Java while loop there are
other ways - while ( loop-test )
- repeated-part
-
- When a while statement is encountered the block
(statements between and ) executes while (as
long as) the loop-test is true
9Flow chart view of while-loop execution
False
True
loop-test
statement-1
Iterative Part
statement-2
statement-n
10 Determinate Loops
- This template repeats a process n times (you fill
in the comments) - int n / how often we must repeat the
process / - int counter 1 while ( counter lt n )
- // the process to be repeated
- counter counter 1
- determinate loops must know the number of
repetitions before they begin know exactly how
many employees, or students, or whatever that
must be processed, for example
11Example for loop that produces an average
- Scanner keyboard new Scanner(System.in)
- double sum 0
- double number
- System.out.print("How many do you want to
average? ") - int n keyboard.nextInt()
- int counter 1
- // Do something n times
- while (counter lt n)
- System.out.print("Enter number ") // lt-
Repeat 3 - number keyboard.nextDouble() // lt-
statements - sum sum number // lt- n times
- counter counter 1 // make sure the loop
stops -
- double average sum / n
- System.out.print("Average of " n " numbers is
" average)
12Active Learning
- Write the output
- int n 5
- int j 1
- while(j lt n)
- System.out.print ( j " " )
- j j 1
-
- j 0
- while(j lt 2 n)
- System.out.print ( j " " )
- j j 2
-
-
137.2 Indeterminate Loops
- Determinate loops have a limitation
- We must know n in advance
- Many situations when repeat a set of statements
an unspecified number of times - Processing report cards for every student in a
school (or paychecks for all employees, or...) - Allowing 1 to many ATM transactions
- Asking the user for specific input and allowing
re-entry of input after invalid inputs - NAME ANOTHER ? ________________ ?
14Some things that terminate indeterminate loops
- An indeterminate loop repeats a process until
some stopping event terminates the repetition - There are many such events, but we'll focus on
these - User enters a special value indicating end of
data - A logical expression becomes false
- The Grid's mover hits the wall or an edge
- The end of a file is encountered
- Indeterminate loops do not need to know n in
advance - Indeterminate loops can actually determine n
15Pattern Indeterminate loop Problem Some process
must repeat an unknown number of times so some
event is needed to terminate the
loop. Algorithm while( the termination event
has not occurred ) execute these
actions bring the loop closer to
termination Code while(myGrid.frontIsClear
()) Example myGrid.putDown( )
myGrid.move( )
16Sentinel Loops
- A sentinel is a specific input from the user to
signal that there is no more data - The sentinel should be the same type of data
- The sentinel must not be in the valid range of
data - Example Use -1 as the sentinel for test scores
that can only be in the range of 0 through 100
inclusive - Enter test scores or -1 to quit
- 80 95 76 82 56 100 45 86 -1
17Sentinel loop continued
- Have to read until the sentinel is read
- process all inputs except the sentinel
- while ( inputValue ! sentinel )
- process inputValue
-
- But where do we input the values?
- check out the code on the next slide
18Sentinel loop complete this dialogue
- Scanner keyboard new Scanner(System.in)
- double sum 0.0
- double inputValue 0.0
- System.out.println("Enter numbers or -1 to quit"
) - while( inputValue ! -1)
- inputValue keyboard.nextDouble( )
- sum inputValue
-
- System.out.println( "Sum " sum )
- Complete this dialogue
- Enter numbers or -1.0 to quit
- 1.0 2.0 3.0 -1.0
- Sum _____ ?
19 Complete the Dialogue -- what is sum?
- The Sum was not correctly computed
- Needed priming read and another read at bottom
- System.out.println("Enter numbers or -1 to quit"
) - currentInput keyboard.nextDouble( )
- while(currentInput ! -1)
- sum inputValue
- currentInput keyboard.nextDouble( )
-
- System.out.println( "Sum " sum )
20Tracing a dialogue AverageComputer.java
- Enter numbers or -1 to quit
- 70.0
- 90.0
- 80.0
- -1.0
- Average 80.0
- Iteration currentInput sum n
currentInput!SENTINEL - Before the loop 70.0 0.0 0 true
- End Loop 1 80.0 70.0 1 true
- End Loop 2 90.0 150.0 2 true
- End Loop 3 -1 240.0 3 false
- After the loop NA 240.0 3 NA
21Infinite loops
- Infinite loop a loop that never terminates.
- Infinite loops are usually not desirable.
- Here is an example of an infinite loop
- test keyboard.nextInt()
- while(test ! -1)
- // now process the test
- sum test
- n
-
- There is no step that brings the loop closer to
termination.