Java For Loop

    Loops are used to execute the set of code or instructions over a number of times when the condition is true. Java supports three types of loops -

    • For
    • While
    • do-while

    Loop Comparison

    Comparison for Loop while Loop do-while Loop
    Introduction This control statement will iterate a part of the programs multiple times. This control flow statement will repeatedly execute a part of the program for a given boolean condition. This control flow statement will execute a part of the program at least once, and the further execution will depend on the given boolean condition.
    When to use It will be preferred If the number of iterations is fixed. It will be used when the number of iterations is not fixed. You can use this loop when the number of iterations is not fixed, and you must have to execute the loop at least once.
    Syntax
    for(init;condition;incr/decr){  
    // code
    }

    while(condition){  
    //code 
    }

    do{  
    //code  
    }while(condition);

    Example
    for(int i=1;i<=10;i++){  
    System.out.println(i);  
    }

    int i=1;  
    while(i<=10){  
    System.out.println(i);  
    i++;  
    }

    int i=1;  
    do{  
    System.out.println(i);  
    i++;  
    }while(i<=10);

    For Loop

    If you know the number of iterations already, you can use the for loop statement to execute the program several times if the condition holds true. There are three types of for loops -

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

    1. Simple for Loop

    The syntax is the same as C or C++ . You can initialize the variable in the for expression, check condition, and increment or decrement the variable's value.

    • Initialization : It is the first condition that will be executed once the loop starts. You can initialize the variable or use an already initialized variable.
    • Condition : It is the second condition that will get executed every time to test the condition of the loop, and execution will be continued until the condition is false. This must return a boolean value, either true or false.
    • Statement : The statement of the loop is executed every time until the second condition is false.
    • Increment/Decrement : It will increment or decrement the variable value.

    Syntax

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

    Example

    public class ForExample {  
    public static void main(String[] args) {  
        for(int i=1;i<=3;i++){  
            System.out.println(i);  
        }  
    }  
    }

    Output

    1
    2
    3

    Nested for Loop

    Java allows you to use one for loop inside another for loop . If the outer for loop holds true value only, the inner for loop will execute.

    Example

    public class NestedForExample {  
    public static void main(String[] args) {  
    //loop of i  
    for(int i=1;i<=3;i++){  
    //loop of j  
    for(int j=1;j<=3;j++){  
            System.out.println(i+" "+j);  
    }//end of i  
    }//end of j  
    }  
    }

    Output

    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    3 1
    3 2
    3 3

    Pyramid Example

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

    Output

    * 
    * * 
    * * * 
    * * * * 
    * * * * *

    2. For-each Loop

    You can use a for-each loop for array or collection traversal. It is easier than using a simple loop within mentioning the increment or decrement value. This loop works on the elements, and each element is returned one by one in the variable.

    Syntax

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

    Example

    public class ForEachExample {  
    public static void main(String[] args) {  
        //Declaring an array  
        int var[]={1,2,44,5,7};  
        //Printing array using for-each loop  
        for(int i:var){  
            System.out.println(i);  
        }  
    }  
    }

    Output

    1
    2
    44
    5
    7

    3. Labeled for Loop

    You can give a name to the for-loop by using a label before the for-loop. It is efficient to use labels if you are working with nested loops.

    Syntax

    labelname:  
    for(initialization;condition;incr/decr){  
    //code 
    }
    Example
    
    public class LabeledForExample {  
    public static void main(String[] args) {  
        aa:  
            for(int i=1;i<=3;i++){  
                bb:  
                    for(int j=1;j<=3;j++){  
                        if(i==2&&j==2){  
                            break aa;  
                        }  
                        System.out.println(i+" "+j);  
                    }  
            }  
    }  
    }

    Output

    1 1
    1 2
    1 3
    2 1

    Infinitive for Loop

    You can create an infinitive loop by using two semicolons in the for-loop. To exit this infinitive loop, press ctrl + c .

    Syntax

    for(;;){  
    //code 
    }

    Example

    public class ForExample {  
    public static void main(String[] args) {  
        for(;;){  
            System.out.println("infinitive loop");  
        }  
    }  
    }

    Output

    infinitive loop 
    
    infinitive loop 
    
    infinitive loop
    
    infinitive loop 
    
    infinitive loop 
    
    ctrl+c