Java Reference
In-Depth Information
The results will be the following:
1 little, 2 little, 3 little Indians
4 little, 5 little, 6 little Indians
7 little, 8 little, 9 little Indians
10 little
16.
Add the following statement after the outer loop:
System.out.print(" Indian boys");
17.
Save and run LoopTestApp.
The results will be the following:
1 little, 2 little, 3 little Indians
4 little, 5 little, 6 little Indians
7 little, 8 little, 9 little Indians
10 little Indian boys
18.
Print out a copy of the LoopTestApp source code.
The LoopTestApp executable source code should be the following:
package c7;
public class LoopTestApp {
public static void main(String[] args) {
int ctr = 1;
while (ctr <= 10) {
for (int i = 1; i <= 3; i++) {
System. out .print(" " + ctr + " little");
ctr = ctr + 1;
if (ctr > 10 || i == 3){
break;
}
System. out .print(",");
}
if (ctr > 10){
break;
}
System. out .print(" Indians");
System. out .println("");
}
System. out .print(" Indian boys");
}
}
Now this may seem like a childish example, but please don't think that nested loops are childish. You will have
many occasions in your programming career to use them.
Search WWH ::




Custom Search