In previous tutorial on java while loop we executed where iteration is fixed now for loop is used where iterations are not fixed.
Java for loop :
When we want to go through a list of elements to process data element by element based on the index position.It’s mostly used in appending data to listview, gridview and android recyclerview.
The syntax of for loop is as
for(initialization; condition; increment/ decrement){ }
For Loop:
So now let’s print 1 to 5 numbers using for loop
for(int i = 0; i < 5; i++){ System.out.println(i); }
we can also slightly adjust the conditions declarations, but need to provide them
public static int i = 0;
for(i = 0; i < 5;){ System.out.println(i); i++; }
Infinite For Loop:
When you missed the incremental/decremental condition then the result will be a infinite loop.
public static int i = 0; public static void main(String args[]) { for(i = 0; i < 5;){ System.out.println(i); // missed incremental condition } }