Java For Loop

The Java for loop is one of the most used loops by the programmer in software development. It provides a compact way of iteration over the range of values.

For loop is categorized as follows in Java:

1) Java Simple for loop

2) Java Enhanced for loop/for-each loop

3) Java Labeled for loop

4) Java Infinite for loop

1) Java Simple For Loop

For loop is used when the number of the iterations is known before entering into the block of the loop. It assigns the variables before entering into the code block of the loop and performs increment at the end of the loop.

Initialization: This is executed once when the loop starts. Users can initialize variable or can use the existing initialized variable. The variable should not be null here.

Boolean Condition: This is the second part of the condition which is executed each time to test the condition. This continues execution if the condition is true. The return value must be boolean. This is an optional one.

Increment/Decrement: this part is increments or decrements the variable value. This is also an optional condition.

Java For Loop | Java Simple For Loop

Syntax

for (initialization; condition; increment or decrement){
	// your code here.
}

public class myforloop {
	// program to print all even numbers till given number
	public static void main(String[] args) {

		int lastNumber = 35;
		for (int i = 0; i < lastNumber; i++) // will run till the last number
		{
			if (i % 2 == 0) // check if value of i is even
			{
				System.out.println("Even Number -" + i);
			}
		}
	}
}
Example Output

2) Java Enhanced For Loop

The enhanced for loop also called as for each loop is an extended feature newly introduced in JAVA 5.0 Release. This feature helps us to retrieve the values from the collection of objects efficiently rather than using indexes.

We can use for each loop in our program in this form:

Syntax:

for(Type identifier : expression){

//statement

}

Where Type represents the type of Object that we are retrieving from the collection, identifier refers to the variable name which will hold the object and expression is the collection object.

To understand for each loop, please look in below program-

Here in Both statements line 1 and line 2 will print the same output:

public class myenhanced
{
    public static void main(String args[])  
    { 
    	int numArray[] = {20, 40, 60, 80};
    	for (int i = 0; i < numArray.length ; i++) // declaration of simple for loop
    	{
    		System.out.println("The array value of index i is = "+numArray[i]); 
    	}
    	System.out.println("*********************");
    	
    	for(int i : numArray) // declaration of for each loop
    	{
    		System.out.println("The array value of index i is = "+i); 
    	}
    	
    } 
}
Example Output

3) Java Labeled Loop

In the nested loop, if we want to break the inner loop within the outer loop, we can pout break statement, and inner loop will exit its execution and continue to outer loop execution.

But what if we want to exit from the outer loop from the inner loop, we can do this by using the labeled loop. That means we give the outer loop a name and we can get exit from any loop by using its name.

public class demo 
{
    public static void main(String args[])  
    { 
    	  int i,j;

          loop1:   for(i=1;i<=20;i++)
          {
              System.out.println();

              loop2:   for(j=1;j<=20;j++)
              {
                  System.out.print(j + " ");

                  if(j==10)
                      break loop1;     
              }
              System.out.println("Outer Loop Terminate");
          }
      }
}
Example Output

4) Infinite For Loop Java

In the for loop, if you removed increment, Boolean Expression, and Increment/Decrement, then it is said to be infinitive for a loop.

Syntex:

for(;;){
//code to be executed
}

public class Demo {
public static void main(String[] args) {
//Using no condition in for loop
for(;;){
System.out.println("Infinite loop");
}
}
}
Example Output

Note: Press Control+c Exit From The Program

Share