3.11. The for statement
Last updated: 23 January 2013.
The for statement allows you to repeat the execution of the same statements a given number of times. For example, if you want to print 5 times "Hello World" to the standard output, a for statement is the solution. A for statement uses an iterator variable to control the repetition of the statements (the loops) it has to execute. Here is the syntax of the for statement:
for ( <initialization> ; <repetition condition> ; <incrementation>){
// the statements that are to be executed repeatedly go here
}
// the statements that are to be executed repeatedly go here
}
Syntax explanation:
- initialization is a statement that initializes the iterator variable.
- repetition condition is a condition that must be true for the statements to be executed. That is, if repetition condition evaluates to true, the statements are executed. Otherwise, the for statement is exited.
- incrementation specifies how the iterator variable is incremented at each iteration.
Create a class named JavaLoops and a method named forLoop as shown below. The method forLoop prints 5 times "Hello World" to the standard output:
public class JavaLoops {
public void forLoop(){
final int MAX_LOOPS = 5;
int i;
for(i=0; i<MAX_LOOPS; ++i){
System.out.println("Hello World");
}
}
}
public void forLoop(){
final int MAX_LOOPS = 5;
int i;
for(i=0; i<MAX_LOOPS; ++i){
System.out.println("Hello World");
}
}
}
Let's take a closer look at the for statement:
- i=0 initializes the iterator variable i.
- i<MAX_LOOPS is the repetition condition. As long as it evaluates to true, "Hello World" will be printed to the standard output.
- ++i increments the iterator variable by one each time "Hello World" is printed to the standard output.
Create a class named App and call the method forLoop:
public class App {
public static void main(String[] args) {
new JavaLoops().forLoop();
}
}
public static void main(String[] args) {
new JavaLoops().forLoop();
}
}
The output is:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
The Java language allows you to declare the iterator variable i within the for statement:
public class JavaLoops {
public void forLoop(){
final int MAX_LOOPS = 5;
for(int i=0; i<MAX_LOOPS; ++i){
System.out.println("Hello World");
}
}
}
public void forLoop(){
final int MAX_LOOPS = 5;
for(int i=0; i<MAX_LOOPS; ++i){
System.out.println("Hello World");
}
}
}
To take another example, the method forLoop2 shown below declares and initializes an array of characters and prints all of its elements to the standard output:
public class JavaLoops {
public void forLoop2(){
char[] myArray = { 'a', 'b', 'c', 'd', 'e' };
for(int i=0; i<myArray.length; ++i){
System.out.println(myArray[i]);
}
}
}
public void forLoop2(){
char[] myArray = { 'a', 'b', 'c', 'd', 'e' };
for(int i=0; i<myArray.length; ++i){
System.out.println(myArray[i]);
}
}
}
The output is:
a
b
c
d
e
b
c
d
e
You are here :
JavaPerspective.com >
Beginner Tutorials >
3. Java Basics >
3.11. The for statement
Next tutorial : JavaPerspective.com > Beginner Tutorials > 3. Java Basics > 3.12. The for-each statement
Next tutorial : JavaPerspective.com > Beginner Tutorials > 3. Java Basics > 3.12. The for-each statement