Bottom-driven Loop

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Yes.

Bottom-driven Loop

All loops must do three things:

  1. The loop must be initialized correctly.
  2. The ending condition must be tested correctly.
  3. The body of the loop must change the condition that is tested.

The code fragment includes all three, but in a different order than usual. The ending condition test is done after the loop body has executed.

int count = 0;                       // initialize count to 0

do
{
  System.out.println( count );      // loop body: includes code to
  count++  ;                        // change the count
}
while ( count < 10 );               // test if the loop body should be
                                    // executed again.

Loops with the test the top of the loop (while and for loops) are called top-driven loops. Loops implemented with the test at the bottom (a do loop) are called bottom-driven loops. This location for the test has some odd (and sometimes undesirable) effects. For example:

int count = 1000;

do
{
  System.out.println( count );
  count++  ;                  
}
while ( count < 10 );

QUESTION 3:

What is the output of the revised loop?