Pre and Post Loop Syntax
Lon Hosford
 
Scottie: "Capitan there is a loop back in the anti-matter containment manifold and we only have 10 seconds until she blows."
 
A few episodes of this famous US TV series dealt with time loops. Somehow the crews of the various Enterprise Starships seem to have escaped all loop predicaments.
 
Hopefully your experience with loops will be less stressful and less life threatening once you have the basic syntax training.
 

 
Loops are code blocks to the compiler as are if statements, switch statements, and functions. So a loop has a beginning and end marker and these are the ubiquitous curly braces. Also the loop block has keywords and logic expressions before the block starting curly brace for pre-test loops and after end curly brace for posttest loops. The logic expression requires open and closed parenthesis. The specialized pre-test counter loop for statement has an assignment expression and counter expression in addition to the logic expression.
 
The assigned reading only discusses pre-test while loops. This posting will show all loop examples and I attached the source. The application is a hard coded multiplication table for the number two and the multipliers 1 to 10.
 

Pre-test while loop:
 
1. public class TestLoop
2. { 
3.    public static void main(String[] args)
4.    {
5.      int multiplier = 1; // Start at 1
6.      while ( multiplier <= 10 )
7.      {
8.         // Output to screen and do multiplication
9.         System.out.println(" 2 times " + multiplier + " = " + (multiplier * 2));
10.        multiplier ++; // Increase by 1
11.     }
12.   }
13. }
 
Notice that the logic condition contains the variable multiplier that changes inside the loop on line 10. The loop would be infinite if this was not the case.

Posttest while loop
 
1. public class TestLoop
2. { 
3.    public static void main(String[] args)
4.    {
5.      int multiplier = 1; // Start at 1
6.      do
7.      {
8.         // Output to screen and do multiplication
9.         System.out.println(" 2 times " + multiplier + " = " + (multiplier * 2));
10.        multiplier ++; // Increase by 1
11.     } while ( multiplier <= 10 );
12.   }
13. }

 
The keyword do on line 6 before the start of the loop block identifies this as a posttest loop. The while, parenthesis and logic expression are moved to after the end of the loop block. Notice the required semi-colon after the close parenthesis.
 
You might consider why have pre-test and posttest loop structures as they seem to do the same in our example. The difference is demonstrated by these examples because they are designed to show syntax and not application. Just remember posttest loops always have 1 iteration and pre-test loops can be skipped. Posttest applications are less frequent and can be coded with a pre-test loop in a few ways such as nested an if statement at the beginning of the loop.
 
For loop 
 
1. public class TestLoop
2. { 
3.    public static void main(String[] args)
4.    {
5.       int multiplier; // No initialization required
6.      for (multiplier = 1;  multiplier <= 10; multiplier ++ )
7.      {
8.         // Output to screen and do multiplication
9.         System.out.println(" 2 times " + multiplier + " = " + (multiplier * 2));
10.     }
11.   }
12. }

The for statement is basically specialized counter pre-test loop. Notice on line 6 that the for statement has a three part section instead of a logic expression enclosed in parenthesis. In a way it looks like three lines of code. Each of these parts uses the same variable which we can call the counter variable. In this case the variable name is multiplier.
 
The first part, multiplier = 1; initializes the counter variable starting value.
 
The second part multiplier <= 10;  is the logic expression designed to express the end of the count. This is important to distinguish this to the logic expression found in the while statement. Logic expression in while statement may not be considering the end of a count. For example a while statement may consider logic of true or false of reaching the end of a file of unknown length. 
 
The last part multiplier ++  is an expression that changes the counter variable in such a way the loop will eventually produce a false value for the logic expression.
 
This is a versatile part of the for statement. You can increment or decrement the counter variable in any conceivable mathematical formula.
Consider multiplier -- for counting backwards by 1. Consider multiplier += 2 for counting by 2.
 
Consider multiplier -= 2 for counting backwards by 2.
 
Consider multiplier += multiplier^2 for counting by the current multiplier to the power of 2 or by 2, 8, and 20 in the above example. Just remember this last part must always be about changing the counter variable.
Also the counter variable may be changed inside the for loop. Be very careful of doing this because you now have two formulas impacting the counter and you can get confused. I point this out because this is done and sometimes a possible programming solution.
 
For example the need to start the loop over from inside the loop may have a selection structure detecting this need and changing the counter variable to a new value or the same starting value. Advice is to resist or consider a while loop instead.