Java Reference
In-Depth Information
statement in Line 5 moves the insertion point to the next line, and so on. This process
continues until i becomes 6 and the loop stops.
What pattern does this code produce if you replace the for statement, in Line 1, with the
following?
for (i = 5; i >= 1; i--)
EXAMPLE 5-20
Suppose you want to create the following multiplication table:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
The multiplication table has five lines. Therefore, as in Example 5-19, we use a for
statement to output these lines as follows:
for (i = 1; i <= 5; i++)
//output a line of numbers
In the first line, we want to print the multiplication table of one, in the second line we want
to print the multiplication table of 2, and so on. Notice that the first line starts with 1 and
when this line is printed, i is 1 . Similarly, the second line starts with 2 and when this line is
printed, the value of i is 2 , and so on. If i is 1 , i * 1 is 1 ;if i is 2 , i * 2 is 2 ; and so on.
Therefore, to print a line of numbers we can use the value of i as the starting number and 10
as the limiting value. That is, consider the following for loop:
for (j = 1; j <= 10; j++)
System.out.printf("%3d", i * j);
Let us take a look at this for loop. Suppose i is 1 . Then we are printing the first line of the
multiplication table. Also, j goes from 1 to 10 and so this for loop outputs the numbers 1
through 10 , which is the first line of the multiplication table. Similarly, if i is 2 ,weare
printing the second line of the multiplication table. Also, j goes from 1 to 10 ,andsothis for
loop outputs the second line of the multiplication table, and so on.
A little more thought produces the following nested loops to output the desired grid:
for (i = 1; i <= 5; i++)
//Line 1
{
//Line 2
for (j = 1; j <= 10; j++)
//Line 3
System.out.printf("%3d", i * j);
//Line 4
System.out.println();
//Line 5
}
//Line 6
Search WWH ::




Custom Search