|
|
1. POST TEST LOOPS |
Where as for a pre-test loop the control variable is tested prior to the start of each iteration in post-test loops the control variable is tested at the end of each iteration (Figure 1). From an efficiency point of view the often stated advantage is that we can reduce the number of tests (although only be 1!) of the loop control variable/parameter provided that we know that the loop must be exercised at least once. If there is a possibility that the loop should not be exercised at all then a post-test loop will not be appropriate (a pre-test loop will be required instead).
Figure 1: Distinction between pre-test and post-test loops
2. THE JAVA "DO-WHILE" LOOP CONSTRUCT |
In Java a post test loop is implement using a "do-while" construct. This has the general form:
Note that the start expression is placed somewhere prior to the start of the construct, and that the update expression must be included in the body of the loop. |
An example of the use of this construct is presented in Table 1 where a trivial piece of Java code is presented containing a post test variable count loop. On each iteration the user is asked to input an integer greater than 0 and less than 51. If a correct input is made the loop terminates. In Table 2 a second piece of Java code is presented which does exactly the same thing but using a while construct. Another example of the same problem is presented in Table 3 using a "for" construct. Note, in Tables 2 and 3 that these programs will test the number variable at least twice (as opposed to at least once using the do-while construct). Also we must initialise the number with an appropriate value to ensure that the user is allowed the opportunity to input a number. |
// VARIABLE COUNT DO WHILE LOOP EXAMPLE APPLICATION // Frans Coenen // Wednesday 5 May 1999 // Revised: Wednesday 23 August 2005 // The University of Liverpool, UK import java.util.*; class DoWhileExample { // ------------------- FIELDS ------------------------ // Create Scanner class instance public static Scanner input = new Scanner(System.in); // ------------------ METHODDS ----------------------- /* Main method */ public static void main(String[] args) { int number; final int MAXIMUM = 50; final int MINIMUM = 1; // Invitation to input System.out.print("Input an integer between " + MINIMUM + " and " + MAXIMUM + " (inclusive): "); // Do-while loop do { number = input.nextInt(); } while (number < MINIMUM || number > MAXIMUM); // End System.out.println("Input = " + number); } |
Table 1: Variable count "do-while" loop example
// VARIABLE COUNT WHILE LOOP EXAMPLE APPLICATION // Frans Coenen // Wednesday 5 May 1999 // Revised: Wednesday 23 August 2005 // The University of Liverpool, UK import java.util.*; class WhileLoopExample { // ------------------- FIELDS ------------------------ // Create Scanner class instance public static Scanner input = new Scanner(System.in); // ------------------ METHODDS ----------------------- /* Main method */ public static void main(String[] args) { int number = 0; final int MAXIMUM = 50; final int MINIMUM = 1; // Invitation to input System.out.print("Input an integer between " + MINIMUM + " and " + MAXIMUM + " (inclusive): "); // Do-while loop while (number < MINIMUM || number > MAXIMUM) { number = input.nextInt(); } // End System.out.println("Input = " + number); } } |
Table 2: Variable count "while" loop example
// VARIABLE COUNT FOR LOOP EXAMPLE APPLICATION // Frans Coenen // Friday 12 November 1999 // Revised: Wednesday 23 August 2005 // The University of Liverpool, UK import java.util.*; class ForLoopExample { // ------------------- FIELDS ------------------------ // Create Scanner class instance public static Scanner input = new Scanner(System.in); // ------------------ METHODDS ----------------------- /* Main method */ public static void main(String[] args) { int number = 0; final int MAXIMUM = 50; final int MINIMUM = 1; // Invitation to input System.out.print("Input an integer between " + MINIMUM + " and " + MAXIMUM + " (inclusive)"); // For loop for (number=0;number < MINIMUM || number > MAXIMUM; ) { number = input.nextInt(); } // End System.out.println("Input = " + number); } } |
Table 3: Variable count "for" loop example
The three pieces of code presented in Tables 1, 2 and 3 above all do the same thing, they allow the user to input a number within a certain range. This is a frequent requirement, however, we would prefer the code to be written in such a way that an error message is displayed whenever an incorrect input is detected. The code presented in Table 4 achieves this. Note that we are using a continuous do-while loop which, in the event of a correct input, we can "break out of" using a
// VARIABLE COUNT DO WHILE LOOP EXAMPLE APPLICATION VERSION 2 // Frans Coenen // Wednesday 5 May 1999 // Revised: Thursday 1 September 2000, Wednesday 23 August 2005 // The University of Liverpool, UK import java.util.*; class DoWhileExampleVer2 { // ------------------- FIELDS ------------------------ // Create Scanner class instance public static Scanner input = new Scanner(System.in); // ------------------ METHODDS ----------------------- /* Main method */ public static void main(String[] args) { int number; final int MAXIMUM = 50; final int MINIMUM = 1; // Invitation to input System.out.print("Input an integer between " + MINIMUM + " and " + MAXIMUM + " (inclusive): "); // Do-while loop do { number = input.nextInt(); if (number >= MINIMUM && number <= MAXIMUM) break; else System.out.println("ERROR: given input (" + number + ") not within range " + MINIMUM + ".." + MAXIMUM + ", try again"); } while (true); // End System.out.println("Input = " + number); } } |
Table 4: Variable count "do-while" loop example with error message
In Tables 1 and 4 above variable count "do-while" loop examples were presented. "Do-while" loops can equally well be used to describe fixed count loops. In Table 5 we present the "smiley faces" program presented previously (using both a fixed count "for" and a fixed count a "while" loop), but in this case implemented using a fixed count "do-while" loop. Note that the test expression, unlike in the previous examples, includes the <= operator so that the same number of smiley faces are output as before. The distinction is that, as this is a post test loop, the loop parameter is incremented before we get to the test expression. Thus if we had not altered the test expression only 9 smiley faces would have been produced instead of 10!
// FIXED COUNT DO-WHILE EXAMPLE APPLICATION // SMILEY FACES 3 // Frans Coenen // Friday 12 November 1999 // The University of Liverpool, UK class SmileyFace { // ------------------ METHODS ----------------------- /* Main method */ public static void main(String[] args) { int loopParameter; final int START_CONDITION = 0; final int END_CONDITION = 10; // Do-while loop loopParameter = START_CONDITION; do { System.out.print("(-:\t"); loopParameter++; } while (loopParameter <= END_CONDITION) // End System.out.println("\n"); } } |
Table 5: Fixed count "do-while" loop example (smiley faces 3)
3. EXAMPLE PROBLEM - FIBONACCI
3.3 DesignFrom Figure 3 the design comprises two classes, Fiobonacci and FibonacciApp.
A Nassi-Shneiderman design for the above is presented in Figure 4. Note how a post test loop is indicated by such a chart. ![]() Figure 4: Nassi-Shneiderman chart for Fibonacci class
Some sample output is given in Table 8
Table 8: Sample output |
Created and maintained by Frans Coenen. Last updated 10 February 2015