Java do-while loop
The do-while
loop in Java is a control structure that allows you to execute a block of code repeatedly, with the key difference being that it guarantees at least one execution of the loop body, regardless of whether the condition is true or false. This is because the condition is checked after the execution of the loop body.
Syntax of the do-while
Loop
do {
// Block of code to be executed
} while (condition);
- Block of Code: The statements within the
do
block are executed once before the condition is checked. - Condition: After executing the loop body, the condition is evaluated. If it evaluates to
true
, the loop body will execute again. If it evaluates tofalse
, the loop terminates.
Example of a do-while
Loop
Here’s a simple example demonstrating how to use a do-while
loop to print the numbers from 0
to 4
:
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 0; // Initialization
// Using a do-while loop to iterate from 0 to 4
do {
System.out.println("Iteration: " + i); // Loop body
i++; // Update
} while (i < 5); // Condition
}
}
Explanation
- Initialization:
int i = 0;
initializes the loop control variablei
to0
. - Loop Body:
System.out.println("Iteration: " + i);
prints the current value ofi
. - Update:
i++;
increments the value ofi
by1
at the end of each iteration. - Condition: After executing the loop body,
while (i < 5);
checks ifi
is less than5
. If true, the loop body will execute again.
Iteration Breakdown
- 1st Iteration: The loop body executes, printing
Iteration: 0
, then incrementsi
to1
. - 2nd Iteration: The loop body executes, printing
Iteration: 1
, then incrementsi
to2
. - 3rd Iteration: The loop body executes, printing
Iteration: 2
, then incrementsi
to3
. - 4th Iteration: The loop body executes, printing
Iteration: 3
, then incrementsi
to4
. - 5th Iteration: The loop body executes, printing
Iteration: 4
, then incrementsi
to5
. - 6th Iteration: The condition
5 < 5
isfalse
, so the loop terminates.
Output
The output of the above program will be:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Important Points
Guaranteed Execution: The
do-while
loop is particularly useful when you need to ensure that the loop body runs at least once, such as in cases where user input is involved, and you want to prompt the user at least once.Infinite Loop: Similar to other loops, if the condition never becomes
false
, the loop can become infinite. For example, if you had a condition that is alwaystrue
, the loop would continue indefinitely.
Example of an Infinite Loop (with correction)
To illustrate the infinite loop and how to correct it:
public class InfiniteDoWhileExample {
public static void main(String[] args) {
int i = 0;
// Infinite loop example (uncomment to see the effect)
do {
System.out.println("Iteration: " + i);
// i++; // Uncommenting this line will prevent infinite loop
} while (i < 5);
}
}
If you run this code without the increment statement (i++
), it will produce an infinite loop, continuously printing Iteration: 0
.
Summary
The do-while
loop is a unique control structure in Java that allows for repeated execution of a code block with the guarantee of at least one execution. Understanding how to use do-while
loops effectively is essential for writing robust Java programs, particularly in scenarios where you want to ensure that a certain block of code is executed before checking a condition.