java iteration
TRANSCRIPT
![Page 1: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/1.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 1/25
©The McGraw-Hill Companies, 2006
ITERATION
(Chapter 3)
![Page 2: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/2.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 2/25
©The McGraw-Hill Companies, 2006
Iteration
• Iteration is the form of program control thatallows us to repeat a section of code
• For this reason this form of control is often alsoreferred to as repetition
• The programming structure that is used tocontrol this repetition is often called a loop
• There are three types of loops in Java: – for loop
– while loop
– do…while loop
![Page 3: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/3.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 3/25
©The McGraw-Hill Companies, 2006
When to use a loop?
• Display a square of stars (5-by-5) on the screen* * * * *
* * * * *
* * * * ** * * * *
* * * * *
• This could be achieved with five outputstatements executed in sequence
• Better to repeat one output statement fivetimes with a loop
![Page 4: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/4.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 4/25
©The McGraw-Hill Companies, 2006
“for” loop
• If we wish to repeat a section of code a fixed number of times (five in the example above) wewould use Java's for loop
• The for loop is usually used in conjunction with
a counter to keep track of how many times wehave been through the loop so far
![Page 5: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/5.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 5/25
©The McGraw-Hill Companies, 2006
Display a 5-by-5 square of stars
![Page 6: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/6.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 6/25
©The McGraw-Hill Companies, 2006
The loop counter
• Very common way of using a for loop: – start the counter at 1
– add 1 to the counter each time the loop repeats
• However
– you may start your counter at any value and
– you may change the counter in anyway youchoose
![Page 7: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/7.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 7/25
©The McGraw-Hill Companies, 2006
A different way of using the loop counter
![Page 8: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/8.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 8/25
©The McGraw-Hill Companies, 2006
Nested loops
![Page 9: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/9.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 9/25
©The McGraw-Hill Companies, 2006
Non-fixed repetitions
• The for loop is an often used construct to implementfixed repetitions
• Sometimes a repetition is required that is not fixed
– a racing game that repeatedly moves a car around a track
until the car crashes – a ticket issuing program that repeatedly offers tickets for
sale until the user chooses to quit the program
– a password checking program that does not let a user intoan application until he or she enters the right password
• The while loop offers one type of non-fixed iteration
![Page 10: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/10.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 10/25
©The McGraw-Hill Companies, 2006
“while” loop
• The syntax for constructing this loop in Java is
• As this loop is not repeating a fixed number of times, there is no need to create a counter tokeep track of the number of repetitions
![Page 11: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/11.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 11/25
©The McGraw-Hill Companies, 2006
Input validation
• Checking input data for errors is referredto as input validation
– For example, asking the user to enter an
exam mark • It should never be greater than 100 or less than 0
![Page 12: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/12.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 12/25
©The McGraw-Hill Companies, 2006
![Page 13: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/13.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 13/25
©The McGraw-Hill Companies, 2006
“do … while” loop
• The do…while loop is another variable loopconstruct
• Unlike the while loop, the do…while loop hasits test at the end of the loop rather than at the
beginning .
• The syntax of a do…while loop is given below:
![Page 14: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/14.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 14/25
©The McGraw-Hill Companies, 2006
Implications of having the test at theend of the loop
• If the test is at the end of the loop, theloop will iterate at least once
• If the test is at the beginning of the loop,however, there is a possibility that thecondition will be false to begin with, andthe loop is never executed
• A while loop therefore executes zero or
more times • A do...while loop executes one or more
times.
![Page 15: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/15.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 15/25
©The McGraw-Hill Companies, 2006
Using the “do … while” loop
• If you want to repeat the statements until theuser chooses to quit your program, you can putthe whole program in a loop
• Involves asking the user each time if he or she
would like to continue repeating your program,or to stop
• A while loop would be difficult to use, as thetest that checks the user's response to a
question cannot be carried out at the beginningof the loop
• The answer is to move the test to the end of theloop and use a do…while loop
![Page 16: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/16.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 16/25
©The McGraw-Hill Companies, 2006
An example program
![Page 17: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/17.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 17/25
©The McGraw-Hill Companies, 2006
![Page 18: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/18.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 18/25
©The McGraw-Hill Companies, 2006
*** Product Price Check ***Enter initial price: 50Enter tax rate: 10Cost after tax = 55.0
Would you like to enter another product (y/n)?: y
*** Product Price Check ***Enter initial price: 70
Enter tax rate: 5Cost after tax = 73.5Would you like to enter another product (y/n)?: Y
*** Product Price Check ***Enter initial price: 200Enter tax rate: 15Cost after tax = 230.0Would you like to enter another product (y/n)?: n
![Page 19: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/19.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 19/25
©The McGraw-Hill Companies, 2006
Menu driven programs
• Another way to allow a program to be runrepeatedly using a do...while loop is toinclude a menu of options within the loop
• The options themselves are processed bya switch statement
• One of the options in the menu list would
be the option to quit and this option ischecked in the while condition of the loop
![Page 20: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/20.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 20/25
©The McGraw-Hill Companies, 2006
Another example
![Page 21: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/21.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 21/25
©The McGraw-Hill Companies, 2006
![Page 22: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/22.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 22/25
©The McGraw-Hill Companies, 2006
![Page 23: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/23.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 23/25
©The McGraw-Hill Companies, 2006
***Lab Times***[1] TIME FOR GROUP A [2] TIME FOR GROUP B[3] TIME FOR GROUP C[4] QUIT PROGRAM
enter choice [1,2,3,4]: 2
1.00 p.m
[1] TIME FOR GROUP A [2] TIME FOR GROUP B[3] TIME FOR GROUP C[4] QUIT PROGRAMenter choice [1,2,3,4]: 5
Options 1-4 only!
[1] TIME FOR GROUP A [2] TIME FOR GROUP B[3] TIME FOR GROUP C[4] QUIT PROGRAMenter choice [1,2,3,4]: 1
10.00 a.m
![Page 24: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/24.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 24/25
©The McGraw-Hill Companies, 2006
[1] TIME FOR GROUP A
[2] TIME FOR GROUP B
[3] TIME FOR GROUP C
[4] QUIT PROGRAM
enter choice [1,2,3,4]: 3
11.00 a.m
[1] TIME FOR GROUP A
[2] TIME FOR GROUP B
[3] TIME FOR GROUP C
[4] QUIT PROGRAM
enter choice [1,2,3,4]: 4
Goodbye
![Page 25: Java Iteration](https://reader035.vdocuments.site/reader035/viewer/2022062420/577d20391a28ab4e1e9248bb/html5/thumbnails/25.jpg)
8/2/2019 Java Iteration
http://slidepdf.com/reader/full/java-iteration 25/25
©The McGraw-Hill Companies, 2006
Picking the right loop
• Use a for loop – If the number of repetitions required can be
determined prior to entering the loop -
• Use a while loop
– If the number of repetitions required cannot bedetermined prior to entering the loop, and you wishto allow for the possibility of zero repetitions
• Use a do...while loop
– If the number of repetitions required cannot bedetermined before the loop, and you require at leastone repetition of the loop