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 execute a part of the program repeatedly 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 iteration already then you can use the for loop statement to execute the program several times if the condition holds true. There are three types of for loop-

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

    Simple for-loop

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

    • Initialization : It is the first condition which will be executed once the loop starts. You can initialize the variable, or you can use an already initialized variable.
    • Condition : It is the second condition which 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 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 then 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-

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

    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

    Labelled 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

    People are also reading: