Home >>Java Tutorial >Java do while Loop

Java do while Loop

Java do-while Loop

The do-while loop in Java is generally used in order to iterate a part of the program for a multiple number of times. The use of do-while loop in Java is often recommended whenever the numbers of iterations are not certain and there is a need to execute the loop at least once as the condition is basically checked after loop body is inspected first.

Syntax

Here is the syntax of the do-while loop in Java depicted below:

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

Here is an example of the do-while loop in Java that will get you to understand the application aspect of the topic

public class do-while {  
public static void main(String[] args) {  
    int i=1;  
    do{  
        System.out.println(i);  
    i++;  
    }while(i<=5);  
}  
} 
Output:
1
2
3
4
5

Java Infinitive do-while Loop

Whenever there is a condition in which the programmer passes true in the do-while loop in the Java language then this will become an infinitive do-while loop.

Syntax

Here is the syntax of do-while loop is depicted below:

do{  
//code that is to be executed  
}while(true);  

Here is an example of the infinitive do-while loop in Java that will get you to understand the working aspect of the topic:

public class do-while {  
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
infinitive do while loop
infinitive do while loop
infinitive do while loop
.
.
.
.

No Sidebar ads