Java for loop iteration syntax

Contributor Icon Contributed by Rex Date Icon September 9, 2004  
Tag Icon Tagged: Java programming

The for loop provides a simple mechanism for repeating a code block a fixed number of times or for iterating through a set of values.


The generic syntax for a Java for loop is:

for ( {initialization}; {exit condition}; {incrementor} )
code_block;

For example, to execute a simple loop 5 times printing out the iteration number each time through the loop:

for ( i=1; i<6; i++ ) {
System.out.println(i);
}

In this example, the loop starts with an initialization of the (int) variable i to the value 1. Each time the loop iteration starts, a the exit condition is evaluated (is i less than 6?) and if true, continues. When the loop iteration is complete, the incrementor code is executed (i++ means increment the variable i by one). The loop continues until the exit condition is met (when i is equal to or greater than 6), so the code block will be executed for i values of 1, 2, 3, 4, and 5.

The exit condition code can be more complex as needed because it is computed each time through the loop, so the condition can be based on variables which may change during the loop iteration. To run a code block on each value of an array, the loop condition could be i < array.length.

The incrementor code can also be more complex, essentially any valid Java code. Another simple example would be a decrementor using i-- which would subtract one from the variable i each time through the loop.

Previous recipe | Next recipe |
 
  • tin2
    better luck next time
  • janak_kumar
    i want many type of for loop code
  • seema
    good
  • Peter
    concerning i < array.length: if you really want to run through each array item, you need to initialize i with 0:
    for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
    }
  • Gobitha
    please give appropriate generic syntax form to this for loop
    "for(int i:arr){System.out.println(i);}"
  • donatjava
    simpl program for loop
  • fiaeriza
    how to do the program and output like this:
    #######
    ######
    #####
    ######
    #####
    ###
    ##
    #
  • usernamer
    please fix it for me!

    /**
    * @(#)hehe.java
    *
    *
    * @author
    * @version 1.00 2009/10/7
    */

    import java.io.*;
    public class hehe{

    public static void main(String args[]) throws IOException {

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    int user;
    int pass;
    int i=1;
    String yesno;
    char yorn;



    while(i<=3){
    System.out.print("Enter Username:");
    user=Integer.parseInt(input.readLine());
    System.out.print("Enter Password:");
    pass=Integer.parseInt(input.readLine());
    for(int j=0;j<=5;j++){
    System.out.println();
    }
    System.out.println();
    if((user==2009)&&(pass==2010)){
    System.out.println("ACCESS GRANTED!");
    for(int j=0;j<=5;j++){
    System.out.println();
    }
    System.out.println();


    }else
    {
    System.out.println("=ACCESS DENIED=");
    System.out.println("Please Try Again");
    for(int j=0;j<=5;j++){
    System.out.println();
    }
    }
    i++;






    String choice;

    System.out.println("Choices:");
    System.out.println("A.PURCHASING ITEMS");
    System.out.println("B.EXIT");
    System.out.print("Enter Your Choice: ");
    choice=input.readLine();
    if (choice.equals("A"))
    {






    }else if(choice.equals("B"))

    {
    System.out.println("Do you really want to exit?");
    System.out.println("Yes(Y)No(N)");
    yesno=input.readLine();
    }




    }

    }
    }
blog comments powered by Disqus