Java for Loop Iteration Syntax

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 as follows:

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

For example, to execute a simple loop five times, printing out the iteration number each time through the loop, use the following syntax:

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, the exit condition is evaluated (Is i less than 6?); and if true, it 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. Thus, 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.

The Conversation

Follow the reactions below and share your own thoughts.

51 Responses to “Java for Loop Iteration Syntax”

  1. January 27, 2009 at 2:18 pm, Anonymous said:

    i want many type of for loop code

    Reply

  2. April 29, 2009 at 8:06 am, seema said:

    good

    Reply

  3. June 22, 2009 at 9:12 pm, Peter said:

    concerning i

    Reply

    • February 28, 2011 at 7:54 am, GameMaster said:

      how about for stacking??

      Reply

  4. June 30, 2009 at 10:03 am, Gobitha said:

    please give appropriate generic syntax form to this for loop
    “for(int i:arr){System.out.println(i);}”

    Reply

  5. July 26, 2009 at 2:29 pm, Anonymous said:

    simpl program for loop

    Reply

  6. August 24, 2009 at 5:03 am, Anonymous said:

    how to do the program and output like this:
    #######
    ######
    #####
    ######
    #####
    ###
    ##
    #

    Reply

    • February 28, 2010 at 9:05 pm, Anonymous said:

      for(int a=5;a>=1;a–)
      {
      System.out.println();
      for(int b=1;b

      Reply

      • January 26, 2011 at 3:16 pm, Shennie Geronimo said:

        nice

        Reply

    • June 11, 2012 at 9:01 am, RICLA said:

      > here is your answer cool baby:)))))))))))
      for(int i=7; i>=5; i–)
      {
      for(int j=0;j=5; i–)
      {
      for(int j=0;j
      =1; i–)
      {
      for(int j=0;j

      Reply

  7. October 12, 2009 at 1:47 am, Anonymous said:

    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

    Reply

    • February 28, 2010 at 9:08 pm, Anonymous said:

      please use switch case, if else statements are not very suitable like this program

      Reply

  8. November 20, 2009 at 2:14 pm, Anonymous said:

    the description is not very good

    Reply

  9. December 31, 2009 at 11:07 am, Anonymous said:

    This page was created in 2004? Is this site ever used or updated? This page could stand to be updated to account for the enhanced for loop which provides a much easier way to traverse an array.

    Reply

  10. February 11, 2010 at 3:04 pm, Anonymous said:

    how to write a java program for following output?
    *
    * * *
    * * * * *
    * * *
    *

    Reply

    • February 28, 2010 at 9:17 pm, Anonymous said:

      for(int a=1;a=1;c–)
      {
      for(int d=1;d

      Reply

  11. February 25, 2010 at 8:25 am, Anonymous said:

    how to program like this using java case
    Create Account:
    Age
    Name
    Address
    Change Pin

    Reply

  12. March 09, 2010 at 4:02 am, Anonymous said:

    how to do this:
    ######
    # #
    # #
    # #
    # #
    ######

    Reply

    • July 13, 2010 at 2:20 am, Da大哥ge said:

      for(int c=1;c

      Reply

  13. July 26, 2010 at 7:31 am, jerome said:

    what is the code for this one:
    5 = *****
    4 = ****
    3 = ***
    2 = **
    1 = *

    —-anybody know?? please help

    Reply

    • August 12, 2010 at 5:13 am, bryan said:

      public class asteris{
      public static void main(String[] args){
      int I=0,j=0;
      while(I

      Reply

  14. October 20, 2010 at 12:06 pm, Pkannan said:

    How to display
    1111
    2222
    3333
    4444
    using for loop?

    Reply

    • February 19, 2011 at 3:23 am, Miracle_ss said:

      class For5
      {
      public static void main(String args[])
      {

      int array[][]=new int[4][5];

      int k=1;
      for(int i=0; i

      Reply

    • May 29, 2012 at 1:20 pm, suresh said:

      public class ForLoop{
      public static void main(String[] args){
      for(int i = 1;i

      Reply

  15. November 12, 2010 at 1:45 am, francis said:

    how about dis one?

    *
    ***
    ****
    *******
    *********

    Reply

  16. November 18, 2010 at 12:17 pm, van said:

    How do I creat a for loop which accepts an integer value/values for the variable age, and displays number from up to the entered number.
    Example:
    Please enter age:3
    lets Count!
    1
    2
    3

    Reply

  17. December 01, 2010 at 3:25 am, Drew T said:

    how do u make a code tht takes a number from the user and adds up by it like if the person was to put 2 it would go 2 4 6 8 all the way up to 100?

    Reply

  18. January 18, 2011 at 11:18 am, Musee21 said:

    if i type 4 then the result…

    ****
    ****
    ****
    ****

    anyone can teach me how?…..i need that program this January 20, 2011..ASAP…please help me..

    Reply

    • February 01, 2011 at 8:35 am, Ajuopel said:

      public static void main(String[]args)
      {
      int a,b,c;
      System.out.println(” press enter number:”);
      a=EasyIn.getInt();
      b=a;
      c=b;
      for( a=2; a

      Reply

  19. January 21, 2011 at 10:17 am, Rea_sweety1990 said:

    *
    **
    ***
    ****
    how to do this

    Reply

  20. January 25, 2011 at 8:00 am, Ajuopel said:

    how 2 program in java if i enter 4 the out put is like this
    4
    ****
    ****
    ****
    ****

    Reply

  21. January 25, 2011 at 9:40 am, Yalmamatter said:

    how to program the ff. output?
    333
    444
    555

    Reply

  22. February 01, 2011 at 4:58 am, Yalmamatter said:

    how to program using for loop,if u input 3 the result is :
    333
    444
    555 if input 2 the result is :
    22
    33 if u input 4 the output is:
    4444
    5555
    6666,,

    Reply

  23. February 01, 2011 at 5:02 am, best programmer of jhcsc said:

    to ajuopel and yalmamatter,,just answer on your own,,,dont depend on me ok??hahhahaha

    Reply

    • February 01, 2011 at 8:05 am, Ajuopel said:

      boang ka bcen borit rna weeeeeeeeeee

      Reply

      • February 08, 2011 at 10:02 am, best programmer of jhcsc said:

        use english language everytime you post your reply here,use this language so that the world can understand you!!!,,i didnt redicule you when i make my reply iim just stating the fact,please learn to grow up and solve your own program?

        Reply

      • February 08, 2011 at 10:07 am, best programmer of jhcsc said:

        use english language everytime you post your reply here,use this language so that the world can understand you!!!,,i didnt redicule you when i make my reply iim just stating the fact,please learn to grow up and solve your own program?

        Reply

  24. February 22, 2011 at 8:44 pm, sri said:

    how to print as follows?
    *
    * *
    * * *
    * * * *

    Reply

  25. February 24, 2011 at 9:44 pm, Parth Thirteen said:

    how do i give the option to exit or to go back to begining, such as a calculator code

    Reply

  26. February 26, 2011 at 9:43 am, Janani_kaushalya said:

    how to write a java program to display following design
    #
    ##
    ###
    ####

    Reply

  27. February 26, 2011 at 9:47 am, Janani_kaushalya said:

    how to write a java program to display following design
    ???#
    ???##
    ???###
    ####

    Reply

  28. May 17, 2011 at 8:01 am, Chris said:

     nice coments

    Reply

  29. June 26, 2011 at 5:18 pm, Amtechxl said:

    how to write a java program to get a o/p as follows?

    1
    12
    123
    1234
    12345

    Reply

  30. July 05, 2011 at 10:55 am, Bob said:

    So, just how many people are looking for homework answers here……?

    Reply

  31. July 09, 2011 at 11:36 am, Rampravesh Mourya said:

    what will be the code of
                          1
                       2     2
                 3        3     3
            4       4         4     4

    Reply

  32. December 04, 2011 at 4:15 am, Nhoriel said:

    How to make a program that it’s output is like this?
    *
    **
    ***
    ****
    *****

    Reply

  33. March 12, 2012 at 11:00 pm, Itayulita said:

    Bagaimana cara penulisan syntak mempunyai hasil seperti :

    number 1 = 12
    number 2 = 32
    number 3 = 10

    nilai yang tertinngi number 2 = 32

    Reply

  34. August 20, 2012 at 6:00 am, shwetha bhat said:

    how to do the program based on the loop for the sum and average.Using (for) condition

    Reply

  35. January 09, 2013 at 9:24 am, janu said:

    easily to understand

    Reply

  36. November 18, 2013 at 3:43 pm, JD said:

    for (String nombre: nombresList) {
    System.out.println(“nnombre #” + (nombresList.indexOf(nombre)+1));
    System.out.println(“t” + nombre);
    }

    Reply

Leave a Reply