4.4 Variations on for Statements

Although most uses of for statements have the simple form that we saw in the last section, many variations are possible. One of the ways in which we can extend the statement is by using commas to combine multiple expressions into larger ones as the next example illustrates.

Example 1

The fragment shown below demonstrates the use of commas to build complex expressions from simpler ones in for statements.
for (i = 0, j = 5; i < j; i++, j--)
   System.out.println(i + " " + j); 

The output from this fragment will be

0 5
1 4
2 3

The initialization here consists of the assignments i = 0 and j = 5 while the modification after each repetition of the loop consists of both an increment of i and a decrement of j.

There is no limit to the number of expressions that can be included in the first and third parts of a for statement. If we want more than two, we can simply add others using more commas. We cannot do this with the second expression; it must be a single boolean expression. If we carry the process of placing multiple expressions in the header of the for to its extreme, we can sometimes produce a loop that has nothing but an empty statement in its body. Such a situation is shown in the next example.

Example 2

This fragment finds and prints the sum of the squares of the numbers from one to ten.
for (i = 1, sum = 0; i <= 10; sum += i*i, i++)
   ;
System.out.println("Sum of squares is " + sum); 

Here the body of the for consists only of an empty statement. We have placed the empty statement's semi-colon on a line by itself so that it is dearly visible to a reader.

Instead of increasing the number of expressions in the first and third parts of a for statement, we can eliminate either or both of them completely if there is no need for explicit initialization or modification.

Example 3

The statement shown below produces a loop that will repeatedly print its message as long as the program is allowed to run.
for ( ; true ; )
   System.out.println("Help! I'm in an infinite loop!"); 

Notice that, although the first and third expressions are empty, the semicolons that are used as separators must still be included.

Exercise 4.4

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


    2. Set the int variable total to the value of

      1 x 100 + 2 x 98 + 3 x 96 + ... + 50 x 2