Home >>Java Tutorial >Java For Loop
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:
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
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); } } }
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); } } } }
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 } } }
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 } } }
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+" "); } } }
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 }
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"); } } }