Java Do While Loop Examples

Use do-while loops. A do-loop avoids the boundary check on the first iteration.
Do-while. Some loops do not need an initial bounds check. A do-while omits this check—it proceeds with the first iteration without checking anything.
By skipping this check, a do-while may perform better than a while-loop. It checks iterations only after the first. But its syntax may be more confusing.While
Example, first iteration. This program uses a do-while loop. It does not check the initial value of the variable i—a program may have no reason to check the initial state.

So: This loop always displays the initial value 10, but then it generates and tests random numbers.

Java program that uses do-while loop import java.util.*; public class Program { public static void main(String[] args) { // Used for the loop. Random random = new Random(); // Use a special value in the first iteration of the loop. int i = 10; do { System.out.println(i); // On following iterations, use a random number. // ... Behavior changes on the second and further iterations. i = random.nextInt(20); } while (i <= 10); } } Output 10 6 8 0
Example, prefix decrement. This example uses a predecrement operation directly in the while-condition. The first value of "i" is known to be greater than or equal to 0.

So: We do not need to check the initial value. But all values after the initial one are checked.

Java program that uses do, prefix decrement public class Program { public static void main(String[] args) { int i = 10; do { // We do not need to check the first iteration's range. // ... Just use it directly with no checks. System.out.println(i); } while (--i >= 0); } } Output 10 9 8 7 6 5 4 3 2 1 0
Performance, expensive bounds. Sometimes a bounds check is expensive and slow. With a do-while loop, we can eliminate the initial check. This optimizes some programs.

Version 1: We benchmark a do-while loop. This version reduces the number of expensive bounds checks.

Version 2: Here we use a while-loop, which performs a bounds check on the first iteration.

Result: When a bounds check is expensive, like expensiveBound(), it improves performance to reduce these checks.

Java program that benchmarks do-loop, expensive bounds public class Program { static int expensiveBound(int number) { // Return 100 in an expensive way. if ((number % 2) == 0) { int a = Integer.parseInt("10"); int b = Integer.parseInt("90"); return a + b; } else { return 100; } } public static void main(String[] args) { System.out.println(expensiveBound(0)); long t1 = System.currentTimeMillis(); // Version 1: use do-loop with expensive bound check. for (int i = 0; i < 100000; i++) { int x = 0; int count = 0; do { count++; x++; } while (x < expensiveBound(i)); // Check validity. if (count != 100) { System.out.println(count); return; } } long t2 = System.currentTimeMillis(); // Version 2: use for-loop with expensive bound check. for (int i = 0; i < 100000; i++) { int count = 0; for (int x = 0; x < expensiveBound(i); x++) { count++; } // Check validity. if (count != 100) { System.out.println(count); return; } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 100 99 ms, do-while with expensiveBounds() 107 ms, for with expensiveBounds()
Code clarity. The do-while loop has advantages in some situations. But it tends to reduce code clarity. This syntax is not as common, and is less expected.
In rare loops, a do-while loop makes the most sense. But often in Java programs, using more methods and reducing loop complexity is a better options.
A review. Do-while eliminates the initial bounds check of a while-loop. The first iteration is always entered. It makes some loops clearer, but is rarely needed.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to