More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next...
-
date post
19-Dec-2015 -
Category
Documents
-
view
219 -
download
0
Transcript of More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next...
![Page 1: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/1.jpg)
More loops
Horstmann Ch 7 continued.
![Page 2: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/2.jpg)
The do-loop
continue-condition?
loop-body statements
next statement
false
true
WHILE-LOOP
continue-condition?
loop-body statements
next statement
false
true
DO-LOOP
![Page 3: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/3.jpg)
Using a do-loopUse this form when you want the loop body to be executed at least once
int data;
int sum = 0;
String dataString;
do
{ dataString=JOptionPane.showInputDialog(null,”enter number:”);
data = Integer.parseInt(dataString);
sum += data;
} while (data != 0);
JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);
![Page 4: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/4.jpg)
Things to know about do-loops
• The loop body will always be executed one
• The first execution is done before the guard is checked
• The do-loop is nothing more than a small variant of the while-loop
• It is often useful for checking user input
![Page 5: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/5.jpg)
When to use the do-loop?int sum;
int data;
String dataString;
dataString=JOptionPane.showInputDialog(null,”enter number:”);
data = Integer.parseInt(dataString);
while(data != 0)
{
dataString=JOptionPane.showInputMessage(null,”enter number:”);
data = Integer.parseInt(dataString);
sum = sum + data; //could have used: sum += data;
}
JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);
![Page 6: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/6.jpg)
A better wayint data;
int sum = 0;
String dataString;
do
{
dataString=JOptionPane.showInputDialog(null,”enter number:”);
data = Integer.parseInt(dataString);
sum += data;
} while (data != 0);
JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);
We don’t have to get the first piece of data, add it to the sum variable, and then start the loop.
![Page 7: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/7.jpg)
The for-loop
• The for-loop is another variant of the while loop
• Every for-loop can be written as a while loop
• Not every while-loop can be written as a for-loop
• Use a for-loop when you know exactly how many times the loop body should be executed
![Page 8: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/8.jpg)
The for loop: Three things in one line
for ( <initialization>; <guard>; <adjustment>)
{
...
}
1. Initialization: (declare and) set a counter, to 0. This is done before everything else.
2. Guard: loop ends when this is false. This is tested before each pass through the loop.
3. Adjustment: e.g. increment a counter. This is done at the end of each pass through the loop.
![Page 9: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/9.jpg)
Examplesfor (int i=0; i<10; i++)
{
JOptionPane.showMessageDialog(null, “ ”+i);
}
int i;
for (i=10; i>0; i--)
{
JOptionPane.showMessageDialog(null, “ ”+i);
}
![Page 10: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/10.jpg)
From while to forint i = startValue;
while (i < endValue)
{
.....
i++;
}
for (int i=startValue; i<endValue; i++)
{
...
}
![Page 11: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/11.jpg)
Flow of control in a for-loop
initialization
test guard
loop body
next statement
adjust
true
false
![Page 12: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/12.jpg)
Rules and guidelines for “for”
• Counters can be declared and initialized in one go
• Never (never) change the value of the counter inside the loop body
• I mean it. Never do that!• If the loop body is one statement only, you
can omit the braces—but please don’t!• Indent the code within the loop body..
![Page 13: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/13.jpg)
Examples// Compute sum = 0.01 + 0.02 + … + 1;
...
double sum = 0;
// Keep adding 0.01 to sum
for (double i=0.01; i <= 1.0 ; i = i+0.01)
{
sum += i;
}
JOptionPane.showMessageDialog("The sum is " + sum);
![Page 14: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/14.jpg)
Common error
• The 3 elements of the for-loop header are sparated by semi-colons, not commas!
• Do this:• for (int i=0; i<10; i++)
• Not this:• for (int i=0, i<10, i++)
• Keep your loops as simple as possible
![Page 15: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/15.jpg)
Nesting for-loops
• Inside the loop body of a for-loop, we can put another for-loop
• Each time through the 1st for-loop, we execute the 2nd loop until its guard is false
• Handy for printing tables like this: 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
![Page 16: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/16.jpg)
Simple examplefor (int i=0; i<5; i++)
{
for (int j=0; j<3; j++)
{
System.out.print(i+“ ”);
}
System.out.println();
}
![Page 17: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/17.jpg)
Nested Loops
• Create triangle pattern
• Loop through rows
[][][][][][][][][][]
for (int i = 1; i <= n; i++){ // make triangle row}
![Page 18: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/18.jpg)
for (int i = 1; i <= n; i++){
// make triangle row
}
for (int i = 1; i <= n; i++){ for (int j = 1; j <= i; j++) { r = r + "[]"; } r = r + "\n"; }
Nested Loops
• Make triangle row is another loop
• Put one loop inside the other → Nested loops
for (int j = 1; j <= i; j++) { r = r + "[]";}r = r + "\n"; \\ “\n” means end of line
![Page 19: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/19.jpg)
/** This class describes triangle objects that can bedisplayed with printed shapes */
public class Triangle{
private int width;
/** Constructs a triangle. @param aWidth number of [] in the last row of triangle. */
public Triangle(int aWidth){ width = aWidth;}
/** Computes a string representing the triangle. @return a string consisting of [] and newline characters */
public String toString(){ String r = ""; for (int i = 1; i <= width; i++){ for (int j = 1; j <= i; j++) { r = r + "[]"; } r = r + "\n"; } return r; }}
![Page 20: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/20.jpg)
File TriangleTester.java
/** This program tests the Triangle class.*/public class TriangleTester{ public static void main(String[] args){ Triangle small = new Triangle(3); System.out.println(small.toString());
Triangle large = new Triangle(15); System.out.println(large.toString()); }}
![Page 21: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/21.jpg)
break-ing out of a loop
• Inside a loop body, the statement break; causes execution to leave the loop immediately.
• This is usually unnecessary, and shows that not enough though has gone into the development of a guard.
![Page 22: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/22.jpg)
Example of break
int sum = 0;
int item = 0;
while (item < 5)
{
item ++;
sum += item;
if (sum >= 6) break;
}
System.out.println("The sum is " + sum);
![Page 23: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/23.jpg)
continue
• This keyword also stops interation of the loop body
• But...the program then starts the next iteration of the loop (testing the loop guard first)
• This is also usually unnecessary, and can be replaced by an if-statement.
![Page 24: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/24.jpg)
Example of continue
int sum = 0;
int item = 0;
while (item < 5)
{
item++;
if (item == 2) continue;
sum += item;
}
System.out.println("The sum is " + sum);
![Page 25: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/25.jpg)
Object-oriented approach to averages/** Computes the average of a set of data values. */public class DataSet{
private double sum; private double maximum; private int count;
/** Constructs an empty data set. */ public DataSet(){ sum = 0; count = 0; maximum = 0; }
/** Adds a data value to the data set @param x a data value */ public void add(double x){ sum = sum + x; if (count == 0 || maximum < x) { maximum = x; } count++; }
![Page 26: More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?](https://reader031.fdocuments.us/reader031/viewer/2022032201/56649d3a5503460f94a14fa1/html5/thumbnails/26.jpg)
(continued)
public double getAverage(){ if (count == 0) { return 0; } else { return sum / count; } }
public double getMaximum(){ return maximum; }} // close the DataSet class
Public class DataSestTester{ public static void main(String[] args){ DataSet myData = new DataSet(); String input; do { input = JOptionPane.showInputDialog(null, “number?”); myData.add(Double.parseDouble(input) ); } while(input.length() > 0); JOptionPane.showMessageDialog(null,“the average of those numbers is ”+ myData.getAverage() ); System.exit(0);}}