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.

  • Anonymous

    i want many type of for loop code

  • seema

    good

  • Peter

    concerning i

    • GameMaster

      how about for stacking??

  • Gobitha

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

  • Anonymous

    simpl program for loop

  • Anonymous

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

    • Anonymous

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

      • Shennie Geronimo

        nice

    • RICLA

      > 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

  • Anonymous

    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

    • Anonymous

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

  • Anonymous

    the description is not very good

  • Anonymous

    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.

  • Anonymous

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

    • Anonymous

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

  • Anonymous

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

  • Anonymous

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

    • Da大哥ge

      for(int c=1;c

  • jerome

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

    —-anybody know?? please help

    • bryan

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

  • Pkannan

    How to display
    1111
    2222
    3333
    4444
    using for loop?

    • Miracle_ss

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

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

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

    • suresh

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

  • francis

    how about dis one?

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

  • van

    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

  • Drew T

    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?

  • Musee21

    if i type 4 then the result…

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

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

    • Ajuopel

      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

  • Rea_sweety1990

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

  • Ajuopel

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

  • Yalmamatter

    how to program the ff. output?
    333
    444
    555

  • Yalmamatter

    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,,

  • best programmer of jhcsc

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

    • Ajuopel

      boang ka bcen borit rna weeeeeeeeeee

      • best programmer of jhcsc

        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?

      • best programmer of jhcsc

        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?

  • sri

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

  • Parth Thirteen

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

  • Janani_kaushalya

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

    • http://www.facebook.com/sampathliyanage Sampath Liyanage

      for(int a=1;a

  • Janani_kaushalya

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

  • Chris

     nice coments

  • Amtechxl

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

    1
    12
    123
    1234
    12345

  • Bob

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

  • Rampravesh Mourya

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

  • Nhoriel

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

  • Itayulita

    Bagaimana cara penulisan syntak mempunyai hasil seperti :

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

    nilai yang tertinngi number 2 = 32

  • shwetha bhat

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

  • janu

    easily to understand

  • JD

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