Lectures / lecture2_2
.pdffor Loop
while loop:
Ccountterr variable initialization moves herre.
int counter = 0;
while ( counter < 4 ) { System.out.println(" *");
counter ++;
} |
|
|
|
Ccounter iincrementt |
|
|
|
||
|
||||
|
|
|
||
|
|
|
|
goeshere.
for loop:
for ( int counter = 0 ; counter < 4 ; counter++ ) {
System.out.println(" |
*"); |
|
Boolean expression |
} |
remains here. |
9 - 11 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Developing a for Loop
Syntax:
for (initialize[,initialize]; boolean_expression; update[,update]) {
code_block;
}
Example:
for (String i = "|", t = "------";
i.length() < 7 ; |
The three |
|
|
||
i += "|", t = t.substring(1) ) { |
parts of the |
|
for loop |
||
|
||
System.out.println(i + t); |
|
|
} |
|
9 - 12 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Topics
•Create a while loop
•Develop a for loop
•Nest a for loop and a while loop
•Use an array in a for loop
•Code and nest a do/while loop
•Compare loop constructs
9 - 13 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Nested for Loop
Code:
int height = 4; int width = 10;
for (int rowCount = 0; rowCount < height; rowCount++ ) {
for (int colCount = 0; colCount < width; colCount++ ) { System.out.print("@");
}
System.out.println();
}
9 - 14 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Nested while Loop
Code:
String name = "Lenny"; String guess = ""; int numTries = 0;
while (!guess.equals(name.toLowerCase())) { guess = "";
while (guess.length() < name.length()) {
char asciiChar = (char)(Math.random() * 26 + 97); guess = guess + asciiChar;
}
numTries++;
}
System.out.println(name + " found after " + numTries + " tries!");
9 - 15 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Topics
•Create a while loop
•Develop a for loop
•Nest a for loop and a while loop
•Use an array in a for loop
•Code and nest a do/while loop
•Compare loop constructs
9 - 16 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Loops and Arrays
One of the most common uses of loops is when working with sets of data.
All types of loops are useful:
•while loops (to check for a particular value
•for loops (to go through the entire array)
•Enhanced for loops
9 - 17 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
for Loop with Arrays
ages (array of int types)
27 |
12 |
82 |
… |
1 |
indexIndexstartsatatzero0. |
Last index of array is |
|||
ages.length – 1. |
||||
|
|
|
ages[i] accesses array values as i goes from 0 to
ages.length – 1.
for (int i = 0; i < ages.length; i++ ) {
System.out.println("Age is " + ages[i] );
}
9 - 18 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Setting Values in an Array
ages (array of int types)
10 |
10 |
10 |
… |
10 |
Loop accesses each element of array in turn.
for (int i = 0; int < ages.length; i++ ) {
ages[i] = 10;
}
Each element in the array is set to 10.
9 - 19 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |
Enhanced for Loop with Arrays
ages (array of int types)
27 |
12 |
82 |
… |
1 |
Loop accesses each |
Each iteration returns |
|
the next element of the |
||
element of array in turn. |
||
array in age. |
||
|
for (int age : ages ) { System.out.println("Age is " + age );
}
9 - 20 |
Copyright © 2011, Oracle and/or its affiliates. All rights reserved. |