- Back to Home »
- Java for Loops
Java's
for
loop enables your programs to repeat a set of operations. A for
loop repeats a block of code as long as some condition is true. Here is a simple example:for(int i=0; i < 10; i++) { System.out.println("I is: " + i); }
This example is a standard
for
loop. Inside the parantheses () after the for
keyword, are three statements separated by semicolon (;).
The first statement declares an
int
variable named i
and assigns it the value 0
. This statement is only executed once, when the for
loop starts.
The second statement compares the value of the
i
variable to the value 10
. If the value of i
is less than 10
, then the for
loop is executed one more time. This statement is executed for each iteration in the for
loop.
The third statement increments the value of
i
. This statement is also executed once per iteration of the for
loop, after the body of the for
loop is executed.
The result of this
for
loop is thus, that the body of the loop is executed 10 times. Once for each of the values of i
that are less than 10 (0 to 9).
You don't actually need the curly braces around the
for
loop body. If you omit the curly braces, then only the first Java statement after the for
loop statement is executed. Here is an example:for(int i = 0; i < 10; i++) System.out.println("i is 1: " + i); // executed inside loop. System.out.println("second line"); // executed outside loop.
In this example, only the first
System.out.println()
statement is executed inside the for
loop. The second System.out.println()
statement is not executed until after the for
loop is finished.
Forgetting the curly braces around the
for
loop body is a common mistake. Therefore it can be a good habit to just always put them around the for
loop body.Loop Initializer, Condition and Post Iteration Operation
As mentioned earlier, the
for
loop contains three statements, separated by semicolons. Here is the example from above, showing the three statements:for(int i=0; i < 10; i++) { System.out.println("I is: " + i); }
These statements each have a different role in the execution of the
for
loop. These roles are:- Loop initializer
- Loop condition
- Post iteration operation
I'll explain the roles in a bit more detail below.
Loop Initializer
The loop initializer statement is only executed once, before the
for
loop begins. The loop initializer statement is typically used to initialize variables or objects that are used by the loop condition statement. In the example above (repeated below) the loop initializer statement (marked in bold) declares an int
variable and assignes it the value 0.for(int i=0; i < 10; i++) { System.out.println("I is: " + i); }
You don't need a loop initializer statement. It is optional. Here is a
for
loop without a loop initializer statement:MyLoop loop = new MyLoop(10); for( ; loop.loopAgain() ; loop.iterationDone()) { }
Notice how an object is used to keep the state controlling the loop. Of course this object could have been declared inside the loop initializer statement. I just moved it outside the
for
loop to show that it is possible.
You can also initialize multiple variables inside the loop initializer statement. Here is an example of that:
for(int i=0, n=10; i < n; i++) { }
Notice how two variables are declared. The
i
variable used as iteration counter, and the n
variable which is used as an iteration boundary. Notice also, how the loop condition now compares the i
variable to the n
variable, instead of to a constant value.Condition
The condition statement is the second statement inside the
for
loop. This statement is an expression that should evaluate to either true or false. If the statement evaluates to true, the for
loop is evaluated one more time. If the statement evaluates to false, the for
loop is not executed anymore, and execution jumps to the first statement after the body of the for
loop.
Here is an example
for
loop with the condition statement marked in bold:for(int i=0; i < 10; i++) { }
Post Iteration Operation
The third statement in the
for
loop is the post iteration statement. This statement is executed after each iteration of the for
loop. This statement is typically used to update the variables that control the condition statement. For instance, increment a counter variable.
Here is an example
for
loop with the post iteration statement marked in bold:for(int i=0; i < 10; i++) { }
The post iteration statement increments the variable
i
. In the condition statement the variable i
is compared to the value 10
. If i
is less than 10
, the for
loop is executed one more time.
The post iteration statement is optional, like the loop initializer statement. Here is an example without post iteration statement:
MyLoop loop = new MyLoop(10); for( ; loop.loopAgain() ; loop.iterationDone()) { }