Java Do While Loop

    Java do-while loop will work when the number of iterations is not known, and the code has to run at least once. This loop will execute the program a number of times until the condition is false. The do-while loop will get executed at least once because the condition is checked after the first iteration, i.e., at the end of the code block.

    Syntax

    do{  
    //code to be executed  
    }
    while(condition);

    Example

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

    Output

    1
    2
    3

    Infinitive do-while Loop

    This loop will execute infinite times if we pass the true condition. You can exit the infinite loop by pressing ctrl+c .

    Syntax

    do{  
    //code to be executed  
    }
    while(true);
    Example
    
    public class DoWhileExample2 {  
    public static void main(String[] args) {  
        do{  
            System.out.println("infinitive do while loop");  
        }while(true);  
    }  
    }

    Output

    infinitive do while loop 
    infinitive do while loop 
    infinitive do while loop  
    ctrl+c