for loop statement in Java

In Previous post we have discussed the Condition Statement. This Article (And next two Articles) will discuss about the Looping statement in Java Language.

Looping statement can be of 3 types.

  1. For Loop
  2. While Loop
  3. Do-While Loop

For Loop

For(for) loop is used when you want to iterate over a range of value.

Scenario : When you want to print a string for 4 times.

Syntax of for loop

 


for (initialization; termination; increment/decrement) {
	//Do Something Here
}
	

initialization : used to initialize the loop. This part will be executed once only.

termination : This loop will continue unless this part has value false.

increment : This expression will be invoked after each iteration can be used to increment or decrements the value.

Example of for loop

 


public class JBT_ForLoopExample {

	public static void main(String[] args) {
		System.out.println("For Loop Example");
		/*
		 * Here we will iterate the loop for 4 number of times and print a
		 * string value
		 */
		int i;
		for(i=0; i<5; i++)
		{
			System.out.println("Current Value of i "+i);
			System.out.println("Termination conditions value is :"+(i<5));
		}

		System.out.println("Outside of loop");
		System.out.println("Termination conditions value is :"+(i<5));
	}
}

	

Output of the above programme would be


For Loop Example
Current Value of i 0
Termination conditions value is :true
Current Value of i 1
Termination conditions value is :true
Current Value of i 2
Termination conditions value is :true
Current Value of i 3
Termination conditions value is :true
Current Value of i 4
Termination conditions value is :true
Outside of loop
Termination conditions value is :false

	

As you can see this loop will continue till termination expression evaluates to FALSE.

Note*:  All three expression in for loop are optional. Means below for loop is also valid.

 


             for(; ; )
		{
                   /// This loop will run infinitly
		}
	

 

Enhanced for Loop

Starting for Java 5, Java has special form of for loop which is mainly useful in case of Collection and Array traversal. Any Class which has implemented iterator interface can use this enhanced for loop.

Syntax of Enhance for loop

 


public class JBT_ForLoopExample {

	public static void main(String[] args) {
		System.out.println("Enhanced For Loop Example");

		int[] intArr = {1,2,3,4,5};
		/*
		 * Using enhanced for loop to iterate the given int array.
		 * 
		 * Left side parameter should be the same as type of Array else compiler will give error.
		 * "cannot convert from element type int to String"
		 */
		for(int j: intArr)
		{
			System.out.println(j);
		}
		System.out.println("-------------------------------------");
		/*
		 * Below enhanced for loop is valid because int can be converted to 
		 * Object. Using Autoboxing 
		 */
		for(Object j: intArr)
		{
			System.out.println(j);
		}

	}

}	

Output of the above code would be


Enhanced For Loop Example
1
2
3
4
5
-------------------------------------
1
2
3
4
5

Here we have learned the for loop. In next article we will learn about other looping statement and that is WHILE loop.

By | 2013-03-17T14:29:26+00:00 March 17th, 2013|Learn By Example|0 Comments

About the Author:

Leave A Comment