The For Loop Statement Tutorial

A statement in Java is a complete unit of execution terminated with a semicolon. A statement can be as simple as a unit of execution like int a = 100; or System.out.println("a =" + a);. Statements inside of a method are executed in a top-down order. What if you want to execute a similar statement over and over and over and ... over. Java provides a set of control flow statements to perform tasks such as these without having to write hundreds of lines of code. This tutorial will introduce you to the basic for statement. I will cover the enhanced for statement in a future tutorial. You will learn how to use the for loop to make a conversion chart for miles-per-hour to kilometers-per-hour.



The for statement structure

The for statement consists of three expressions.
The expressions are contained inside the set of parenthesis located directly after the Java keyword for.
The three expressions are separated by semicolons.
The first expression is called the initialization expression and it is only executed once when the loop begins.
The second expression is called the termination expression and it is only executed at the beginning of each loop. If the termination expression evaluates to false, program execution exits the for loop.
The last expression is called the increment expression and it is invoked after each iteration of the loop.

for (initialization; termination; increment) {
    Statements are executed as long as the termination expression is equal to true;
}


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ForStatement
C:\Java>cd ForStatement
C:\Java\ForStatement>Notepad ForStatement.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ForStatement {    
    public static void main(String args[]) {
        System.out.println("Let's begin the for loop.");
        for(int i=0; i < 5; i++) {
            System.out.println("i = " + i);
        }
        System.out.println("The first for loop is complete.");
        System.out.println();

        System.out.println("Let's begin the second for loop.");
        for(int i=4; i < 4; i++) {
            System.out.println("i = " + i);
        }
        System.out.println("The second for loop is complete.");
        System.out.println();

        System.out.println("Let's begin the third for loop.");
        for(int i=5; i >=0; i--) {
            System.out.println("i = " + i);
        }
        System.out.println("The Third for loop is complete.");
    }
}

Now switch back to the command prompt (CMD) and type in javac ForStatement.java and press Enter.
Now type in java ForStatement and press Enter.


C:\Java\ForStatement>javac ForStatement.java
C:\Java\ForStatement>java ForStatement
Let's begin the for loop.
i = 0
i = 1
i = 2
i = 3
i = 4
The first for loop is complete.

Let's begin the second for loop.
The second for loop is complete.

Let's begin the third for loop.
i = 5
i = 4
i = 3
i = 2
i = 1
i = 0
The Third for loop is complete.


Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class ForStatement {    
    public static void main(String args[]) {
        int milesPerHour;
        double kilometersPerHour;

        System.out.println("MPH   KPH");
        for(milesPerHour = 25; milesPerHour <= 50; milesPerHour++) {
            kilometersPerHour = milesPerHour * 1.609344; // there are 1.609344 kilometers in a mile
            System.out.println(milesPerHour + "    " + kilometersPerHour );
        }
    }
}

Now switch back to the command prompt (CMD) and type in javac ForStatement.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\ForStatement>javac ForStatement.java
C:\Java\ForStatement>java ForStatement
MPH   KPH
25    40.2336
26    41.842944
...
49    78.857856
50    80.4672

Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class ForStatement {    
    public static void main(String args[]) {
            // All three expressions are optional
            System.out.println("Let's create an infinite loop"); //!!! press CTRL+C to escape from the infinite loop one you start it
            for( ; ; ) {
                    System.out.println("Looping forever and ever! CTRL-C to escape.");
            }
    }
}

Now switch back to the command prompt (CMD) and type in javac ForStatement.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\ForStatement>javac ForStatement.java
C:\Java\ForStatement>java ForStatement
Looping forever and ever! CTRL-C to escape.
Looping forever and ever! CTRL-C to escape.
Looping forever and ever! CTRL-C to escape.
...

Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class ForStatement {    
    public static void main(String args[]) {
            // All three expressions are optional, let's set only the termination expression
            
            int i = 0;
            for( ; i < 5 ; ) {
                    System.out.println(i);
                    i++;
            }
    }
}

Now switch back to the command prompt (CMD) and type in javac ForStatement.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\ForStatement>javac ForStatement.java
C:\Java\ForStatement>java ForStatement
0
1
2
3
4

Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class ForStatement {    
    public static void main(String args[]) {
            // All three expressions are optional, let's set only the increment expression
            
            int i = 0;
            for( ; ; i++) {
                    System.out.println("CTRL-C to escape, i = " + i);
            }
    }
}

Now switch back to the command prompt (CMD) and type in javac ForStatement.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\ForStatement>javac ForStatement.java
C:\Java\ForStatement>java ForStatement
CTRL-C to escape, i = 0
CTRL-C to escape, i = 1
CTRL-C to escape, i = 2
CTRL-C to escape, i = 3
...

Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class ForStatement {    
    public static void main(String args[]) {
            // All three expressions are optional, let's set only the increment expression
            // introducing the Break Java keyword
            int i = 0;
            for( ; ; i++) {
                    System.out.println("i = " + i);
                    if(i>=5) {
                        break; // the Java break keyword will break out of the for loop 
                               // and program execution will continue at the end of the for code block.
                    }
            }
            System.out.println("We broke out of the for loop!");
    }
}

Now switch back to the command prompt (CMD) and type in javac ForStatement.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\ForStatement>javac ForStatement.java
C:\Java\ForStatement>java ForStatement
i = 0
i = 2
i = 3
i = 4
i = 5
We broke out of the for loop!


Final thoughts

The for statement makes repetitive tasks very simple to perform. Understanding how the three expression statements control the flow of the iterations of a for statement is a valuable piece of knowledge.


Tutorials