CMPS 134
Notes on Java's for-loop

Almost every computer program that does something interesting has one or more segments of code that are executed repeatedly. The programming language constructs that support this are loops and recursion. Here we concentrate on loops. Specifically, we focus on Java's for-loop, which is one of three kinds of loops that exist in that language.

In a proper usage of a for-loop, there is a loop control variable (LCV) (typically, but not necessarily, of type int) that varies over a specified range of values. For each value v in that range, a code segment (called the loop body) is executed while the LCV has value v. Each execution of the loop body is referred to as an iteration of the loop.

The syntax of a Java for-loop is

for (<LCV-initialization>; <loop guard>; <LCV-update>)
{
   <loop body>
}

The LCV-initialization is an assignment statement by which is established the initial value of the LCV. Often the assignment is combined with the LCV's declaration, in which case the LCV's scope includes only the loop (i.e., its heading and body). The loop guard is a boolean expression (i.e., one that evaluates to either true or false) involving the LCV that is used to determine when no more iterations should take place. The LCV-update is an assignment to the LCV (typically to increment or decrement it by one, or possibly by two or more) that is to be carried out at the end of each loop iteration.

What follows is a flowchart illustrating flow-of-control through a for-loop.

                                +-------+
                                | begin |
                                +-------+
                                    |
                                    |
                                    v
                            +----------------+
                            |     LCV-       |
                            | initialization |
                            +----------------+
                                    |
                                    |
      +---------------------------> |
      |                             v
      |                            / \
      |                           /   \
      |                          /     \
      |                  true   / loop  \     false     +-----+  
      |             +<---------(  guard  )------------> | end |
      |             |           \       /               +-----+
      ^             |            \     /
      |             v             \   /
      |       +-----------+        \ /
      |       | loop body | 
      |       +-----------+     
      |             |
      |             |
      |             v
      |        +--------+
      |        |  LCV-  |
      +<-------| update |
               +--------+ 


Examples

Each row of the following table shows a Java code segment featuring a for-loop and the output it produces when executed.

Code SegmentOutput ProducedRemarks
for (int i=1; i<=5; i = i+1) {
   System.out.println("i is " + i);
}
i is 1
i is 2
i is 3
i is 4
i is 5
Counts up from 1 to 5
for (int i=4; i>0; i = i-1) {
   System.out.println("i is " + i);
}
i is 4
i is 3 
i is 2
i is 1
Counts down from 4 to 1
int n = 5;
for (int i=1; i <= 2*n-1; i = i+2) {
   System.out.println("i is " + i);
}
i is 1
i is 3 
i is 5
i is 7
i is 9
Prints the first n positive odd integers (1 through 2n-1 by 2's)
int n = 5;
for (int i=1; i <= n; i = i+1) {
   System.out.println("i is " + (2*i - 1));
}
i is 1
i is 3 
i is 5
i is 7
i is 9
Prints the first n positive odd integers in a different way.
int n = 6;
int sumSoFar = 0;
for (int i=1; i <= n; i = i+1) {
   sumSoFar = sumSoFar + i;
   System.out.print("i is " + i);
   System.out.println("; sumSoFar is " + sumSoFar);
}
i is 1; sumSoFar is 1
i is 2; sumSoFar is 3
i is 3; sumSoFar is 6
i is 4; sumSoFar is 10
i is 5; sumSoFar is 15
i is 6; sumSoFar is 21
Reports each sum 1+2+...+k, for k in range 1..n.
int n = 6;
int sumSoFar = 0;
for (int i=1; i <= n; i = i+1) {
   sumSoFar = sumSoFar + 2*i - 1;
   System.out.print("i is " + i);
   System.out.println("; sumSoFar is " + sumSoFar);
}
i is 1; sumSoFar is 1
i is 2; sumSoFar is 4
i is 3; sumSoFar is 9
i is 4; sumSoFar is 16
i is 5; sumSoFar is 25
i is 6; sumSoFar is 36
Reports each sum 1+3+...+(2k-1) (i.e., the sum of the first k odd positive integers), for k in range 1..n.
for (char ch = 'A'; ch <= 'G'; ch = (char)(ch+1)) {
   System.out.print(ch);
}
ABCDEFG
Prints the characters in the range 'A' .. 'G' all on one line. Uses casting to convert the result of expression ch+1 from a value of type int to one of type char.
for (int i = (int)('A'); i <= (int)('G'); i = i+1) {
   System.out.print((char)i);
}
ABCDEFG
Prints the characters in the range 'A' .. 'G' all on one line, same as previous code segment. Uses an LCV of type int instead of type char.
for (int i = 0; i <= 6; i = i+1) {
   System.out.print((char)('A' + i));
}
ABCDEFG
Prints the characters in the range 'A' .. 'G' all on one line, same as previous code segment.
for (int i = 1; i <= 4; i = i+1) {
   System.out.println("Beginning iteration with i = " + i);
   for (int j=1; j <= 3; j = j+1) {
      System.out.println("   i is " + i + "; j is " + j);
   }
   System.out.println("Ending iteration with i = " + i);
}
Beginning iteration with i = 1
   i is 1; j is 1
   i is 1; j is 2
   i is 1; j is 3 
Ending iteration with i = 1
Beginning iteration with i = 2
   i is 2; j is 1
   i is 2; j is 2
   i is 2; j is 3 
Ending iteration with i = 2
Beginning iteration with i = 3
   i is 3; j is 1
   i is 3; j is 2
   i is 3; j is 3 
Ending iteration with i = 3
Beginning iteration with i = 4
   i is 4; j is 1
   i is 4; j is 2
   i is 4; j is 3 
Ending iteration with i = 4
The outer loop iterates four times, with i=1,2,3,4. During each iteration of the outer loop, the nested loop iterates three times, with j=1,2,3
for (int i = 1; i <= 4; i = i+1) {
   System.out.println("Beginning iteration with i = " + i);
   for (int j=1; j <= i; j = j+1) {
      System.out.println("   i is " + i + "; j is " + j);
   }
   System.out.println("Ending iteration with i = " + i);
}
Beginning iteration with i = 1
   i is 1; j is 1
Ending iteration with i = 1
Beginning iteration with i = 2
   i is 2; j is 1
   i is 2; j is 2
Ending iteration with i = 2
Beginning iteration with i = 3
   i is 3; j is 1
   i is 3; j is 2
   i is 3; j is 3 
Ending iteration with i = 3
Beginning iteration with i = 4
   i is 4; j is 1
   i is 4; j is 2
   i is 4; j is 3 
   i is 4; j is 4
Ending iteration with i = 4
The outer loop iterates four times, with i=1,2,3,4. During each iteration of the outer loop, the nested loop iterates i times, with j=1,2,...,i