Java for loop

Description

Java for loop statement provides a powerful way of writing loop statement.

Syntax

The simplest form of the for loop is shown here:


   for(initialization; condition; iteration) 
     statement; 

Java for loop statement has three parts:

  • initialization sets a loop control variable to an initial value.
  • condition is a Boolean expression that tests the loop control variable. If condition is true, the for loop continues to iterate. If condition is false, the loop terminates.
  • The iteration determines how the loop control variable is changed each time the loop iterates.

Example

Here is a short program that illustrates the for loop. i is the loop control variable and i is initialized to zero in the initialization. At the start of each iteration, the conditional test x < 10 is performed. If the outcome of this test is true, the println() statement is executed, and then the iteration portion of the loop is executed. This process continues until the conditional test is false.


public class Main {
  public static void main(String args[]) {
    int i;/*from w  w  w  .j av  a2 s . c o m*/

    for (i = 0; i < 10; i = i + 1)
      System.out.println("This is i: " + i);
  }
}

This program generates the following output:

Example 2

The following code writes the code logic from above again but loops reversively:

 
public class Main {
  public static void main(String args[]) {
    for (int n = 10; n > 0; n--)
      System.out.println("n:" + n);
  }/*from w ww.  java  2 s .  c  om*/
}  ]]>

The output:

Example 3

Here is a program that tests for prime numbers using for loop statement.

 
public class Main {
  public static void main(String args[]) {
    int num;//w  ww .  j a  v a 2 s.  c  om
    boolean isPrime = true;
    num = 50;
    for (int i = 2; i <= num / 2; i++) {
      if ((num % i) == 0) {
        isPrime = false;
        break;
      }
    }
    if (isPrime)
      System.out.println("Prime");
    else
      System.out.println("Not Prime");

  }
}

The output:

Example 4

Java allows two or more variables to control a for loop. And you can include multiple statements in both the initialization and iteration portions of the for loop. Each statement is separated from the next by a comma. Here is an example:

 
public class Main {
  public static void main(String args[]) {
/* w  w  w .ja  v  a  2s . c  om*/
    for (int a = 1, b = 4; a < b; a++, b--) {
      System.out.println("a = " + a);
      System.out.println("b = " + b);

    }
  }
}

The program generates the following output:

Example 5

The three sections of the for can be used for any purpose and parts of the for loop can be empty.

 
public class Main {
  public static void main(String args[]) {
    int i = 0;/*from  w w  w .  j  a  v  a  2s  .  co m*/
    boolean done = false;
    for (; !done;) {
      System.out.println("i is " + i);
      if (i == 10)
        done = true;
      i++;

    }
  }
}

The output:

Example 6

for loop can be nested to produce powerful logic, for example, we can use nested for loop to iterate a two-dimensional array. For example, here is a program that nests for loops:

 
public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < 10; i++) {
      for (int j = i; j < 10; j++)
        System.out.print(".");
      System.out.println();//  w w  w  . java2 s.  c  om
    }
  }
}

The output produced by this program is shown here:





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures