for Loop

The simplest form of the for loop is shown here:


   for(initialization; condition; iteration) statement; 

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.

Here is a short program that illustrates the for loop:


public class Main {
  public static void main(String args[]) {
    int i;

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

This program generates the following output:


This is i: 0 
This is i: 1 
This is i: 2 
This is i: 3 
This is i: 4 
This is i: 5 
This is i: 6 
This is i: 7 
This is i: 8 
This is i: 9 

i is the loop control variable.

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.

The following code loops reversively:

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

The output:


n:10
n:9
n:8
n:7
n:6
n:5
n:4
n:3
n:2
n:1

Control Variables Inside the for Loop

It is possible to declare the variable inside the initialization portion of the for.

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

The output:


tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1

The scope n ends when the for statement ends. Here is a program that tests for prime numbers using for loop statement.

  

public class Main {
  public static void main(String args[]) {
    int num;
    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:


Not Prime

Using the Comma

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.
Each statement is separated from the next by a comma.
Here is an example:
  
public class Main {
  public static void main(String args[]) {

    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:


a = 1 
b = 4 
a = 2 
b = 3

For Loop Variations

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

 
public class Main {
  public static void main(String args[]) {
    int i = 0;
    boolean done = false;
    for (; !done;) {
      System.out.println("i is " + i);
      if (i == 10)
        done = true;
      i++;

    }
  }
}

The output:


i is 0
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10

Declare multiple variables in for loop Example

You can define more than one variables and use them inside the for loop.

 
public class Main {
  public static void main(String[] args) {
    for (int i = 0, j = 1, k = 2; i < 5; i++)
      System.out.println("I : " + i + ",j : " + j + ", k : " + k);
  }
}

 

The output:


I : 0,j : 1, k : 2
I : 1,j : 1, k : 2
I : 2,j : 1, k : 2
I : 3,j : 1, k : 2
I : 4,j : 1, k : 2

Nested for Loops

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();
    }
  }
}  

The output produced by this program is shown here:


.......... 
......... 
........ 
....... 
...... 
..... 
.... 
... 
.. 
.
Home 
  Java Book 
    Language Basics  

Statement:
  1. Simplest if statement
  2. If else statement
  3. switch statement
  4. while loop
  5. do-while statement
  6. for Loop
  7. for each loop
  8. break to Exit a Loop
  9. continue
  10. return statement returns from a method.
  11. Comments