Java For Loop Sample Code

Pretty much any Java program or application you write will most likely need at least one for loop in it. The last Java app I wrote had a ton of for loops. They are common and needed most of the time, especially when processing any sort of data. It’s good to know that any variable that is created inside the for loop is only visible within the for loop.

Java Basic For Loop Increment Sample

The basic or standard for loop is known by every Java programmer. This for loop has 3 parts to it. First part initializes the variable ctr and sets it to 0. The second variable is the condition statement, which defines ctr to be less than 10. The last part if to either increment or decrement the ctr. The result below prints ctr from 0 to 9. Remember Java indexes start at 0.

//Simple increment for loop 0 - 9
for(int ctr=0; ctr < 10; ctr++)
{
	System.out.println("Increment Loop Ctr: "+ctr);
}
Looping on ctr: 0
Looping on ctr: 1
Looping on ctr: 2
Looping on ctr: 3
Looping on ctr: 4
Looping on ctr: 5
Looping on ctr: 6
Looping on ctr: 7
Looping on ctr: 8
Looping on ctr: 9


Java Basic For Loop Decrement Sample

The first for loop incremented up from 0 to 9. Maybe we want to go in reverse. This for loop will start from 9 and decrement down to 0.

//Simple decrement for loop 9 - 0
for(int ctr=9; ctr >= 0; ctr--)
{
	System.out.println("Decrement Loop Ctr: "+ctr);
}
Decrement Loop Ctr: 9
Decrement Loop Ctr: 8
Decrement Loop Ctr: 7
Decrement Loop Ctr: 6
Decrement Loop Ctr: 5
Decrement Loop Ctr: 4
Decrement Loop Ctr: 3
Decrement Loop Ctr: 2
Decrement Loop Ctr: 1
Decrement Loop Ctr: 0


Java Basic For Loop Even Numbers

What if you only wanted to print even numbers or loop through even records? The flexibility of the Java language is what makes it so popular with programmers. This task can be easily accomplished by making adjustments to the for loop. The loop ctr is initialized to 2, the condition is set to less than 21, and the ctr is increments by 2 instead of just 1.

//Simple loop print even numbers from 2 - 20
for(int ctr=2; ctr < 21; ctr+=2)
{
	System.out.println("Even Loop Ctr: "+ctr);
}
Even Loop Ctr: 2
Even Loop Ctr: 4
Even Loop Ctr: 6
Even Loop Ctr: 8
Even Loop Ctr: 10
Even Loop Ctr: 12
Even Loop Ctr: 14
Even Loop Ctr: 16
Even Loop Ctr: 18
Even Loop Ctr: 20


Java Enhanced For Loop Increment Sample

The Java enhanced for loop is considered to be a little more elegant than the standard for loop. It is most useful when iterating over some form of an Array. As you see here, we define an array of integers and use the enhanced method to loop through each integer.

//Create an array of integers to loop through
int ctrs[] = {0,1,2,3,4,5,6,7,8,9};
		
//Loop using the enhanced loop
for(int ctr : ctrs )
{
	System.out.println("Enhanced Loop (Increment) Ctr: "+ctr);
}
Enhanced Loop (Increment) Ctr: 0
Enhanced Loop (Increment) Ctr: 1
Enhanced Loop (Increment) Ctr: 2
Enhanced Loop (Increment) Ctr: 3
Enhanced Loop (Increment) Ctr: 4
Enhanced Loop (Increment) Ctr: 5
Enhanced Loop (Increment) Ctr: 6
Enhanced Loop (Increment) Ctr: 7
Enhanced Loop (Increment) Ctr: 8
Enhanced Loop (Increment) Ctr: 9


Java Enhanced For Loop Decrement Sample

As you can see, we can achieve the same results as the standard for loop with this decrement sample by reversing the order of the integers in the Array. Just as with the basic for loop, any variable created within the for loop is not visible outside the for loop. We can see that here with int ctr.

//Create an array of integers to loop through
int ctrs[] = {9,8,7,6,5,4,3,2,1,0};
		
//Loop using the enhanced loop
for(int ctr : ctrs )
{
	System.out.println("Enhanced Loop (Decrement) Ctr: "+ctr);
}
Enhanced Loop (Decrement) Ctr: 9
Enhanced Loop (Decrement) Ctr: 8
Enhanced Loop (Decrement) Ctr: 7
Enhanced Loop (Decrement) Ctr: 6
Enhanced Loop (Decrement) Ctr: 5
Enhanced Loop (Decrement) Ctr: 4
Enhanced Loop (Decrement) Ctr: 3
Enhanced Loop (Decrement) Ctr: 2
Enhanced Loop (Decrement) Ctr: 1
Enhanced Loop (Decrement) Ctr: 0


Java Enhanced For Loop String Array Sample

This sample is an example that you can define any type of Array and the enhanced for loop will treat it like any other object. Define the Array of Strings and iterate though the list.

//Create an array of String to loop through
String strs[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
		
//Loop through each element of String Array
for(String str : strs)
{
	System.out.println("Each String: "+str);
}
Each String: Zero
Each String: One
Each String: Two
Each String: Three
Each String: Four
Each String: Five
Each String: Six
Each String: Seven
Each String: Eight
Each String: Nine


Concluding Comments

Java has provided two options for loops. The basic for loop has been along forever and certainly is the best option in some circumstances. The enhanced for loop was introduced in Java 5 and for me is the best option when needing to loop through an Array.

Add a Comment

Your email address will not be published. Required fields are marked *