codescracker


java

Java for Loop



« Previous Tutorial Next Tutorial »


Beginning with the JDK 5, there are two forms of the for loop. The first is the traditional form that has been in use since the original version of Java. And the second is the newer "for-each" (you will learn about this loop in detail in other chapter) form.

We will discuss both types of the for loop one by one, i.e., one in this chapter and the newer one in the next chapter.

Java for Loop Syntax

Following is the general form of the traditional for statement :

for(initialization; condition; iteration)
{
	// body of the loop
}

If only one statement is being repeated, then there is no need for the curly braces. But you can use curly braces (since it makes easier to add some other statements later).

Java for Loop Working

The working of for loop is explained here:
When the loop starts, then first, the initialization portion of the loop is executed (only once). In general, this is an expression which sets the value of the loop control variable, which acts as a counter which controls the loop.
It is important to understand that, as already told, the initialization expression is executed first but only once. Next, the condition is evaluated. This (the condition expression) must be a Boolean expression. It generally tests the loop control variable against a target value. If this expression becomes true, then the body of the loop is executed. If it is false, then the loop terminates. And next, the iteration portion of the loop is executed. This is generally an expression that increments or decrements the variable which controls the loop. The loop then iterates, first evaluating the conditional expression, then executing the loop's body, and then executing the iteration expression with each and every pass. This process repeats until the controlling expression becomes false.

Java for Loop Example

Following is a version of the "tick" program that uses a for loop :

/* Java Program Example - Java for Loop
 * This program demonstrate the for loop 
 */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        int i;
        
        for(i=10; i>0; i--)
        {
            System.out.println("tick " + i);
        }
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java for loop

Let's look at one more example, also demonstrates the for loop:

/* Java Program Example - Java for Loops
 * This program demonstrates for loop
 */

public class JavaProgram
{ 
   public static void main(String args[])
   {
       
      int i;
      
      for(i=1; i<=15; i++)
      {
          System.out.println(i + " ");
      }
      System.out.println("Fire....!!!");  
      
   }
}

The sample run is shown here :

for loop in java

More Examples

Here are some list of example programs that uses for loop :


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test