Java for loop

In this chapter you will learn:

  1. What is Java for loop
  2. How to write for loop
  3. Example - A simple for loop
  4. Example - loops reversively
  5. Example - Calculate the prime number with for loop
  6. Example - How to use comma in the for loop statement
  7. How to write different forms of for loop
  8. How to write nested for loops

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;/*  ww w  .ja  v a  2  s .  com*/

    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);
  }// w  ww .  j ava  2s.  c o m
}  ]]>

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;/*from  ww  w  .ja  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[]) {
//from ww  w. j  av  a 2s.com
    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;/* w ww . ja  v a 2s  .  c o  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();//from   w ww  . java 2s  . com
    }
  }
}

The output produced by this program is shown here:

Next chapter...

What you will learn in the next chapter:

  1. What is Java for each loop
  2. How to write for each loop
  3. Example - for each loop
  4. Example - for-each style loop to iterate a two-dimensional array
  5. Example - search an array element with for each loop
Home »
  Java Tutorial »
    Java Langauge »
      Java Statement
Java if Statement
Java if else Statement
Java if else ladder statement
Java nested if statement
Java switch Statement
Java for loop
Java for each loop
Java while Loop
Java do while loop
Java break statement
Java continue statement
Java Comments
Java documentation comment(Javadoc)