Exercise 4.4
- Assuming that all variables are of type int, determine the output of
each fragment.
for (i = 1, j = 12; i < j; i++, j -= 2)
System.out.println(i + " " + j);
for (i = 1,product = 1; i <= 5; product *= i,i += 2)
System.out.println(product);
for (n=5,i=1; n>l; System.out.println(i),n--,i*=2)
;
- Rewrite each fragment as a single for statement whose body consists
of only an empty statement (as illustrated in Example 2).
product = 1;
for (int i = 10; i > 0; i--)
product *= i;
max = i = 0;
j = 10;
for ( ; i < 10 ; )
if (i * j > max)
max = i++ * j--;
Write a for statement that has the form of the for statement in
Example 2 and that performs the indicated action.
- Set the double variable sum to the value of
- Set the int variable total to the value of
1 x 100 + 2 x 98 + 3 x 96 + ... + 50 x 2
| |