Home
Map
do while Loop ExampleUse do while loops, which will avoid the boundary check on the first iteration.
Java
This page was last reviewed on Jan 20, 2024.
Do loop. Some loops in Java programs 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.
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.
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); } }
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.
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); } }
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.
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); } }
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.
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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 20, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.