For Loops in java

For Loop statements are an important part of nearly any programming language because they provide a way to repeatedly execute some task.
The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are two types of for loop in java.
1) Simple for loop
2) For-each / Enhanced For Loop


Simple for loop

Simple for loop is used to repeat the particular statement .

  Flow chart of For loop:

syntax:

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

Simple example of for loop.


//example to print "hello java " 5 times.
class Example
{
    public static void main(String[] args)
    {
        for (int i=1;i<=5 ;i++ ) {
            System.out.println("hello java");
        }
        
    }
}
hello java
hello java
hello java
hello java
hello java

Another example of for loop.


//example to print numbers from 1 to 10 using for loop.
class Example
{
    public static void main(String[] args)
    {
        for (int i=1;i<=10 ;i++ ) {
            System.out.println(i);
        }
        
    }
}
1
2
3
4
5
6
7
8
9
10

Another example of for loop.


//example to print the table of 2 .
class Example
{
    public static void main(String[] args)
    {
        for (int i=1;i<=10 ;i++ ) {
            System.out.println(2+" * "+i+" = "+(2*i));
        }
        
    }
}
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

Another example of for loop.


/*example of putting two or more than two variable in 
intialization , condition  or inc/dec.
*/
class Example
{
    public static void main(String[] args)
    {
        for (int i=1,j=1;i<=10 &&j<=5 ;i++,j++ ) {
            System.out.println(2+" * "+i+" = "+(2*i));
        }
        
    }
}
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10

For-each /Enhanced-for loop

A for-each style loop is designed to traverse array or collection in sequential fashion , from start to finish. In Java, the for-each style of for is also referred to as the enhanced for loop.

Syntax of for-each loop:


    for(Type t : array){  
    //code to be executed  
    }  

Example to traverse array using for-each loop.


/*example of for each loop to traverse array*/
class Example
{
    public static void main(String[] args)
    {
        int num[]={1,22,32,43,54,234};
        for(int x:num)
        {
            System.out.println(x);
        }
        
    }
}
1
22
32
43
54
234

Example to traverse a Collection using for-each loop.


/*example of for each loop to traverse Collection*/
//you will lean more about Collecton in collection Framework chapter
import java.util.ArrayList;
class Example
{
    public static void main(String[] args)
    {
        ArrayList list=new ArrayList();
        list.add(12);
        list.add(32);
        list.add(33);
        //now traverse the ArrayList 
        for(Object x:list)
        {
            System.out.println(x);
        }
        
    }
}
12
32
33

Using Label for loop.

We can also use Label for loop to control the flow. Let us see the example.

// example of Label for loop
import java.util.ArrayList;
class Example
{
    public static void main(String[] args)
    {
      Outer :
         for (int i=1;i<=10 ;i++ ) {
             Inner :
                for (int j=1;j<=10 ;j++ ) {
                    if(j==4)
                    {
                        break Outer;//breaking the outer loop using label
                    }else
                    {
                        System.out.println(i+" "+j);
                    }
                
                }
          
      }         
    }
}
1 1
1 2
1 3