QR Code for this Page
Java Looping or Iteration Statements

Java Looping or Iteration Statements

Looping or Iteration Statements in Java

  • for
  • while
  • do - while

for loop in Java

Syntax :
for(<initialization>;<condition>;<increment>)
{
	Statement 1;

	Statement 2;

	Statement n;
}
Tips for Java for Loop
  • If you ignore the braces after loop statement the first statement after loop is executed in loop.
  • If you put ; (semi colon) to loop without braces no statement is executed.
  • If you put ; (semi colon) to loop with braces the body of the loop is executed once.

The for statement also has another form designed for iteration through Collections and Arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.

We recommend using enhanced for statement instead of the general for statement whenever possible.

Java for loop Example

/*
    Java for loop Example
    Save with file name ForLoop.java
*/

public class ForLoop
{
	public static void main(String args[])
	{
		System.out.println("Java for loop Example ");

		for(int count=1 ; count<=10 ; count++)
		{
			// POST INCREMENT

			System.out.println("Loop Statement : " + count);
		}

		System.out.println("Loop 2 output");

		// ANOTHER WAY

		int i = 0;

		for(;i<10;)
		{
			// PRE INCREMENT WITH IN THE BODY OF THE LOOP

			System.out.println("Loop Statement : " + ++i);
		}

		System.out.println("Loop 3 output");

		// LOOP WITH NO BODY 

		int j=0;

		for(;j<10;j++); // ; (SEMICOLON) IGNORE THE BODY OF THE LOOP
		{
			System.out.println("loop body ignore");
		}

		System.out.println("j value after loop execution : " + j);
	}
}

Java Enhanced for loop Example

/*
    Java Enhanced for loop Example
    Save with file name ForLoop.java
*/

public class EnhancedForLoop
{
	public static void main(String args[])
	{
		System.out.println("Java Enhanced for loop Example ");

		int[] numbers = {1,2,3,4,5,6,7,8,9,10};

		for (int item : numbers)
		{
		    System.out.println("Count is: " + item);
		}
	}
}

Java while loop

Syntax :
while(<condition>)
{
	Statement 1;

	Statement 2;

	Statement n;
}

Java while loop Example

/*
    Java while loop Example
    Save with file name WhileLoop.java
*/

public class WhileLoop
{
	public static void main(String args[])
	{
		System.out.println("Java while loop Example ");

		int count = 1;

		while(count<=10)
		{
			// POST INCREMENT

			System.out.println("Loop Statement : " + count);

			count++;
		}
	}
}

Java do while loop

The statements within the do while loop are executed at least once. So the do while loop is called bottom tested loop.

Syntax :
do
{
	Statement 1;

	Statement 2;

	Statement n;

} while(<condition>);

Java do while loop Example

/*
    Java do while loop Example
    Save with file name DoWhileLoop.java
*/

public class DoWhileLoop
{
	public static void main(String args[])
	{
		System.out.println("Java do while loop Example ");

		int count = 1;

		do
		{
			//POST INCREMENT

			System.out.println("Loop Statement : " + count);

			count++;

		} while(count<=10);
	}
}