Java Reference
In-Depth Information
LISTING 6.3
continued
// Print a specific number of values per line of output
count++;
if (count % PER_LINE == 0)
System.out.println();
}
}
}
OUTPUT
Enter a positive value: 7
Enter an upper limit: 400
The multiples of 7 between 7 and 400 (inclusive) are:
7 14 21 28 35
42 49 56 63 70
77 84 91 98 105
112 119 126 133 140
147 154 161 168 175
182 189 196 203 210
217 224 231 238 245
252 259 266 273 280
287 294 301 308 315
322 329 336 343 350
357 364 371 378 385
392 399
The Stars program in Listing 6.4 shows the use of nested for loops. The
output is a triangle shape made of asterisk characters. The outer loop executes
exactly 10 times. Each iteration of the outer loop prints one line of the output.
The inner loop performs a different number of iterations depending on the line
value controlled by the outer loop. Each iteration of the inner loop prints one
star on the current line. Writing programs that print variations on this triangle
configuration are included in the programming projects at the end of the chapter.
The for-each Loop
A variation of the for statement, often called the for-each loop , is particularly
helpful in situations that involve iterators. In Chapter 5 we discussed that some
 
Search WWH ::




Custom Search