Home >>Java Tutorial >Java For Loop

Java For Loop

For Loop in Java

The For loop in java is basically used when there is a need to iterate a part of the program several times. The use of for loop in java is recommended when the number of iteration is generally certain.

The For loop in java is generally of three types that are depicted below:

  • Simple For Loop
  • For-each or Enhanced For Loop
  • Labeled For Loop

1. Simple For Loop in Java

A simple for loop in Java language is basically the same in functioning as of the C/C++ language. The programmers can generally initialize the variable to check the condition and increment/decrement the value. It basically has the four parts that are depicted below

  • Initialization: As the name suggests, it is basically the initial condition that is generally executed for only one time when the loop starts. Here, either the variable is initialized by the users or the user can make the use of an already initialized variable. However, this is an optional condition.
  • Condition: This is basically the second condition that is generally executed each time in order to test the condition of the loop. Until the condition comes to be as false, this continues to execute the condition. This is also an optional condition and it is known to surely return the boolean value that is either true or false.
  • Statement: The statement of the loop is generally executed every time until the second condition becomes false.
  • Increment/Decrement: This part basically increments or decrements the variable value. And this is also an optional condition.

Syntax:

Here is the syntax of the For loop in Java as depicted below:

for(initialization;condition;incr/decr)
{  
//statement or code to be executed  
}  

Here is an example of the for loop in java that will explain about the printing the number from 1 to 15

public class ForEx 
{  
public static void main(String[] args) 
{  
    for(int i=1;i<=15;i++)
	{  
        System.out.println(i);  
    }  
}  
}
Output : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Here is another example of the for loop in java that will print even number from 1 to 100

public class ForEx 
{  
public static void main(String[] args) 
{  
    for(int i=1;i<=100;i++)
	{  
		if(i%2==0)
		{
        System.out.println(i);
		}		
    }  
}  
}
Output : 2 4 6 8 10 12 14 16 18 20............... 100

Java Nested For Loop

A situation where the programmers have a for loop inside another loop then this situation is generally known as the nested for loop in the Java language. Whenever outer loop executes then only the inner loop executes itself completely.

Here is an example of the for loop in java that will make you understand the complete analysis of it:

public class NestedForEx{  
public static void main(String[] args)
{  
	for(int i=1;i<=4;i++)
	{  
		for(int j=1;j<=i;j++)
		{  
			System.out.println(" * ");  
		}
		System.out.println();//for new line	
	} 
 }  
}
Output :
*
* *
* * *
* * * *

Here is another example of the Java Nested For Loop

public class NestedForEx1 
{  
public static void main(String[] args) 
{  
	for(int i=1;i<=3;i++)
	{  
	for(int j=1;j<=i;j++)
	{  
		System.out.print(i+" ");  
	}  
	System.out.println(); //this is for new line  
	}  
	}  
}   
Output :
1
1 2
1 2 3
1 2 3 4

2. For-each Loop in Java

The for-each loop in java language is basically used to traverse array or the collection that are present in java. As the programmer doesn’t require to increment value and use the subscript notation hence, it is termed as easier to use as compared to the simple loop. It generally performs on the elements basis not on the basis of index. This for-each loop generally returns elements one by one in the defined variable or you can say, sequence wise.

Syntax:

Here is the syntax of the For-each loop in Java that is depicted below:

for(Type var:array)
{  
//code that is to be executed  
}

Here is an example of the For-loop in Java that will make you understand the topic from the application point of view:

public class ForeachEx{  
public static void main(String[] args) 
{  
    int array[]={10,11,12,13,14,15};  
    for(int i:array)
	{  
        System.out.println(i+" ");  
    }  
}  
}  
Output :
10
11
12
13
14
15

3. Labeled For Loop in Java

There is sometimes a requirement to have a name of each for loop in Java and in order to do so generally the process that is followed is to use label before the for loop. It has proven to be useful in case the users have nested for loop that allows the users to break/continue specific for loop. Generally, the break and continue keywords do the breaking/continuing of only the innermost for loop.

Syntax

Here is the syntax of the labeled for loop in Java that is depicted below:

labelname:  
for(initialization;condition;incr/decr)
{  
//code that is to be executed  
}

Java Infinitive For Loop

Whenever in a condition where there is use of two semicolons (;;) in the for loop then this will be called as the infinitive for loop in the Java language.

Syntax

Here is the syntax of the infinitive For Loop in Java that is depicted below:

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

Here is an example of the Infinitive For Loop in Java that will depict the use of the infinitive for loop just by printing an statement:

public class ForEx{  
public static void main(String[] args) 
{  
    //There is no any condition defined in for loop  
    for(;;)
	{  
        System.out.println("statement will print infinitive times");  
    }  
}  
}
Output :
statement will print infinitive times
statement will print infinitive times
statement will print infinitive times
statement will print infinitive times
.
.
.
infinite times...

Popular Tutorials