Introduction
The do-while
loop is a control flow statement in Java that allows repetitive execution of a block of code. Unlike the while
loop, the do-while
loop ensures that the block of code is executed at least once before checking the condition. This makes it useful for situations where the code block needs to be executed at least once regardless of the condition. In this chapter, we will explore the syntax, usage, and examples of the do-while
loop in Java.
Syntax
The basic syntax of the do-while
loop is as follows:
do {
// code to be executed
} while (condition);
Key Points:
- The code block inside the
do
is executed once before the condition is tested. - After executing the code block, the
condition
is evaluated. - If the
condition
is true, the code block is executed again. - This process repeats until the
condition
becomes false.
Example
Let’s consider an example where we use the do-while
loop to print numbers from 1 to 5.
Example Code:
public class DoWhileExample {
public static void main(String[] args) {
int count = 1;
do {
System.out.println("Count: " + count);
count++;
} while (count <= 5);
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Infinite Loop
A do-while
loop can become an infinite loop if the condition never becomes false. This can happen if the loop control variable is not updated correctly.
Example of Infinite Loop:
public class InfiniteDoWhileLoopExample {
public static void main(String[] args) {
int count = 1;
do {
System.out.println("Count: " + count);
// Missing count++;
} while (count <= 5);
}
}
In this example, the count
variable is never incremented, so the condition count <= 5
remains true indefinitely, causing an infinite loop.
Using break in do-while Loop
The break
statement can be used to exit the loop prematurely, regardless of the condition.
Example with break:
public class BreakInDoWhileExample {
public static void main(String[] args) {
int count = 1;
do {
if (count == 3) {
break;
}
System.out.println("Count: " + count);
count++;
} while (count <= 5);
}
}
Output:
Count: 1
Count: 2
Using continue in do-while Loop
The continue
statement skips the current iteration of the loop and proceeds with the next iteration.
Example with continue:
public class ContinueInDoWhileExample {
public static void main(String[] args) {
int count = 1;
do {
if (count == 3) {
count++;
continue;
}
System.out.println("Count: " + count);
count++;
} while (count <= 5);
}
}
Output:
Count: 1
Count: 2
Count: 4
Count: 5
Diagram: Flow Chart of do-while Loop
Start
|
[initialize]
|
[execute]
|
[update/check condition]
|
/ \
True False
/ \
| End
|
[execute]
|
[update/check condition]
|
True/False (loop continues)
Conclusion
The do-while
loop is a useful control flow statement in Java that ensures a block of code is executed at least once before checking the condition. This is particularly useful in scenarios where you need the code block to run at least once regardless of the condition. By understanding the syntax and usage of the do-while
loop, including how to manage infinite loops and use break
and continue
statements, you can write more flexible and efficient Java programs.