Java do-while Loop: Syntax and Usage Explained
Table of Content:
Do While Loop
A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. do-while Statement is Exit Controlled Loop because condition is check at the last moment. In do-while loop body gets executed once whatever may be the condition but condition must be true if you need to execute body for second time.
do { // Loop body; Statement(s); } while (loop-continuation-condition);
Braces { }:
The curly braces are unnecessary if only a single statement is being repeated.
Loop body:
In do-while loop body gets executed once whatever may be the condition but condition must be true if you need to execute body for second time. A one-time execution of a loop body is referred to as an iteration (or repetition) of the loop
loop-continuation-condition:
The condition can be any Boolean expression.
The loop body is executed first, and then the loop-continuation-condition is evaluated. If the evaluation is true , the loop body is executed again; if it is false , the do - while loop terminates.
Example of while loop:
// Demonstrate the do-while loop. class DoWhile { public static void main(String args[]) { int n = 0; do { System.out.println("Number " + n); n++; } while(n < 10); } }
Output:
Number 0 Number 1 Number 2 Number 3 Number 4 Number 5 Number 6 Number 7 Number 8 Number 9 Press any key to continue . . .
Infinitive do-while Loop in Java
If you pass true in the do-while loop, it will be infinitive do-while loop.
do{ //code to be executed } while(true);
Example:
public class InfiniteDoWhileLoop { 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 infinitive do while loop infinitive do while loop infinitive do while loop ....................... ....................... ....................... ....................... infinite time it will print like this
Difference Between while and do-while Loop
Key Differences Between while and do-while Loop:
- The while loop checks the condition at the starting of the loop and if the condition is satisfied statement inside the loop, is executed. In the do-while loop, the condition is checked after the execution of all statements in the body of the loop.
- If the condition in a while loop is false not a single statement inside the loop is executed, and if the condition in ‘do-while’ loop is false then also the body of the loop is executed at least once then the condition is tested.
BASIS FOR COMPARISON | WHILE | DO-WHILE |
---|---|---|
General Form | while ( condition) { statements; //body of loop } |
do{ . statements; // body of loop. . } while( Condition ); |
Controlling Condition | In 'while' loop the controlling condition appears at the start of the loop. | In 'do-while' loop the controlling condition appears at the end of the loop. |
Iterations | The iterations do not occur if, the condition at the first iteration, appears false. | The iteration occurs at least once even if the condition is false at the first iteration. |
Point to Remember
Both while and do-while loop are the iteration statement, if we want that first, the condition should be verified, and then the statements inside the loop must execute then the while loop is used. If you want to test the termination condition at the end of the loop, then the do-while loop is used.