Java for Loop

The article is about the Java for loop and it’s uses.

The Java for loop is used to create a loop that runs a certain amount of times, until the given condition has been met. This is in contrast to it’s counter parts, the while loop and do while loop which iterate for an unspecified number of times as long as a condition is met.

For loops in Java are more flexible and can be manipulated to a greater degree than for loops in languages like VB.NET or Python. For loops in Java also have a variant called the For each loop, sometimes referred to as the enhanced for loop. We’ll discuss that here briefly too.

Make sure to use the correct loop for your program. Evaluate the pros and cons of each loop and be clear on what you require.


Syntax

The Java syntax for the for loop is identical to that of the for loop in C++.

Below is the general form of the for loops in Java. Emphasis on general form, as for loops in Java can be heavily manipulated by changing, adding and removing parameters.

for(initialization; condition; increment)
{
statement sequence
}
  • initialization: This is where the loop control variables gets declared and initialized. Typically all loop control variables are initialized from zero.
  • condition: The condition that must become false for the For Loop to end.
  • increment: Defines the increment of the loop control variables every iteration.

Simple for loop

This the Java for loop in it’s most basic and most commonly used form. A simple initialization starting from 0, a given condition, and an increment of 1 after each iteration.

public class example {
	public static void main(String[] args) {		
		for (int i = 0; i < 10; i++) {
			System.out.println("Value of i is " + i);
                }
	}
}

This purpose of this loop is simply to repeat 10 times.

Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
Value of i is 5
Value of i is 6
Value of i is 7
Value of i is 8
Value of i is 9

Removing a parameter

Remember, the condition parameter can never be removed. It is necessary for a loop to have a condition. Similarly, the initialization step must not be skipped either. However, we may skip the 3rd parameter (to some degree).

First we shall demonstrate running a loop without the third parameter.

for (int i = 0; i < 10;) {								
	System.out.println("Value of i is " + i);
}

If the above is written in the above style, it will result in an infinite loop. It has a condition, but that condition will never be fulfilled. The value of i is not increasing at all. (In some cases, an infinite loop might actually be useful).

Hence, to achieve a more normal for loop, we increment the value of i within the code block as shown below. This loop is now the same as the first example shown on this page.

for (int i = 0; i < 10;) {					
	System.out.println("Value of i is " + i);
	i = i + 1;
}

Multiple control variables

One of the unique features of the Java for loop is the ability to have multiple initializations for multiple variables. See the example below to understand further.

public class example {
	public static void main(String[] args) {
		int i, j;
			
		for (i = 0, j = 10; i <= j; i++, j--) {
			System.out.println(i + " - " + j);	
                }	
	}
}

The above code produces the following output. The loop terminates when the given condition has been fulfilled, that i must be greater or equal to j.

0 - 10
1 - 9
2 - 8
3 - 7
4 - 6
5 - 5

continue statement

The continue statement will stop the current iteration of a loop and skip to the next, avoiding any statements in between. We will use the first example of the for loop, and add in the continue statement to observe its effects.

public class example {
	public static void main(String[] args) {
				
		for (int i = 0; i < 10; i++) {						
			if (i % 2 == 0) {
				continue;
			}
			System.out.println("Value of i is " + i);			
		}
	}
}
Value of i is 1
Value of i is 3
Value of i is 5
Value of i is 7
Value of i is 9

The above code will keep skipping the System.out statement while the value of i is an even number. Hence, all the odd numbers from 0 to 9 are printed.


Break statement

The break statement is used for pre-mature termination of a loop. As soon the loop encounters the break statement, the loop will stop its execution and move on to the next statement after the loop.

public class example {
	public static void main(String[] args) 	{		
				
		for(int x = 0; x < 10; x++ )
		{
			if(x == 5) break;
			System.out.println(x);
		}		
	}
}
0
1
2
3
4

The above code terminates the loop once the value of x reaches 5.


This marks the end of the Java for loop article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments