In this article we will see looping in Java, loops are used to execute a block of code saveral times. There are three types of loops in Java, for loop, while loop and do-while loop. We will see their syntax and usage with example in rest part of this article.


1) for loop

2) while loop

3) do-while loop

4) Extended for loop


1) The for loop in Java

In java for loop is mostly used loop, a for loop is useful when we know that how many number of times a block of code needs to be executed. Below is the basic syntax of an for loop:

for(initialization; expression; update)
{
  	 //Statements
}


In the code avobe, initialization is the part where we can declare and initialize the index variable i.e. i=0. This place is optional, we can have a varible initialization here or can keep it blank.

In the middle, expression is the condition that is responsible for loop continuity, loop keeps on running untill the expression is true. This is again an optional field.

Once loop body is executed, update part runs and changes expression variables as per declaration, most of the time an increment ot decrement is done here for expression variable. This is again an optional field.

Example of for loop in Java

Here is an example of for loop, we can execute any block of code saveral times as shown below:

package com.beingjavaguys.example;

public class Test {
	// loop to print numbers from 0 to 10
	public static void main(String args[]) {
		for (int i = 0; i <= 10; i++) {
			System.out.println("Loop Index: " + i);
		}
	}
}

Output

Loop Index: 0
Loop Index: 1
Loop Index: 2
Loop Index: 3
Loop Index: 4
Loop Index: 5
Loop Index: 6
Loop Index: 7
Loop Index: 8
Loop Index: 9
Loop Index: 10



2) The while loop in Java

In java a while loop is used when we are not aware of exact occurance a code block should execute. While loop keeps on repating block code execution untill the condition is true. Below is the basic syntax of an while loop:

while (condition) {
	//statements
}
While loop keeps on executing the code in uts body untill the consition is true or a break statement comes in between. We will see about break in few minutes.

Example of while loop in Java

Here is an example of while loop, we can execute any block of code saveral times as shown below:

package com.beingjavaguys.example;

public class Test {
	// loop to print numbers from 0 to 10
	public static void main(String args[]) {
		int i = 0;
		while (i <= 10) {
			System.out.println("Loop Index: " + i);
			i++;
		}
	}
}

Output

Loop Index: 0
Loop Index: 1
Loop Index: 2
Loop Index: 3
Loop Index: 4
Loop Index: 5
Loop Index: 6
Loop Index: 7
Loop Index: 8
Loop Index: 9
Loop Index: 10



3) The do-while loop in Java

The do-while loop works as a while loop, except that it guarantees the block of code will be executed at least once no matters of the condition in while os true or not. In do-while loop the bosy is executed once and than it determines the condition to ensure if the body should execute again or not. Below is the basic syntax of an while loop:

do {
	// Statement
   } while (condition);

Example of do-while loop in Java

Here is an example of do-while loop, we can execute any block of code saveral times as shown below:

package com.beingjavaguys.example;

public class Test {
	// loop to print numbers from 0 to 10
	public static void main(String args[]) {
		int i = 0;
		do {
			System.out.println("Loop Index: " + i);
			i++;
		} while (i <= 10);
	}
}

Output

Loop Index: 0
Loop Index: 1
Loop Index: 2
Loop Index: 3
Loop Index: 4
Loop Index: 5
Loop Index: 6
Loop Index: 7
Loop Index: 8
Loop Index: 9
Loop Index: 10



4) The extended for loop in Java

From Java 5 one more version of for loop is introduced to iterate over collections and arrays, that is called extended for loop. Below is the syntax of extended for loop:

for (current_element_in_iteration : array_or_collection) {
	// Statments
}
In this kind of for loop we passed array or collection in the condition right to the colon (:), and a variable is declared to the left of colon(:). In each iteration variable holds the value of current value in passed array or collection. The loop keeps on executing until all the elements are exhausted in the array or a break statement appears.

Example of extended-for loop in Java

Here is an example of extended for loop, we can iterate any array or collection as shown below:

package com.beingjavaguys.example;

public class Test {

	// loop to print numbers from 0 to 10
	public static void main(String args[]) {
		int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		for (int i : numbers) {
			System.out.println("Loop Index: " + i);
		}
	}
}

Output

Loop Index: 0
Loop Index: 1
Loop Index: 2
Loop Index: 3
Loop Index: 4
Loop Index: 5
Loop Index: 6
Loop Index: 7
Loop Index: 8
Loop Index: 9
Loop Index: 10


In this particular article we came across basic Java Loops, for loop, while loop and do-while loop. We came to know syntax and few examples. In upcoming articles we will see more about Java and Related technologies.
  • By Techburps.com
  • Feb 28, 2015
  • Core Java