Control Structures: Loops

while Loops

The generalized grammar for a while loop is as follows. First, you must begin with while, followed directly by a set of parenthesis and a conditional expression within the parenthesis. Recall that a conditional expression will yield a true or false result or will simply be the literal true or false. Next, the while and parenthesis will be followed by a connected statement block, marked by the opening and closing pair of curly brackets. Example syntax is listed here.
Here is the example structure of the while loop.
  // initialization of the variable used to control the loop

  while (expression) {
    // the statements to execute each time the loop iterates
    // included is a specific statement which increments the loop variable
  }
Here is the sequence of steps of the while loop.

The order used is to complete step 1. Then to follow the sequence of step 2, step 3, and step 4. Once step 4 is finished, return to step 2 and repeat the sequence.
  step 1

  while (step 2) {
    step 3
    step 4
  }
Here is an example code fragment of a while loop that will print from zero to a user defined stopping point.
  i = 0;
  while (i < stop) {
    System.out.println("i = " + i);
    i++;
  }

do while Loops

The generalized grammar for a do while loop is as follows. First, you must begin with do, followed by a connected statement block, marked by the opening and closing pair of curly brackets. After the statement block, while and a conditional expression within parenthesis are listed. Recall that a conditional expression will yield a true or false result or will simply be the literal true or false. The closing syntax of the entire do while loop structure is a semi-colon, placed after the closing parenthesis of the conditional expression. Example syntax is given here.
Here is the example structure of the do while loop.
  // initialization of the variable used to control the loop

  do {
    // the statements to execute each time the loop iterates
    // included is a specific statement which increments the loop variable
  } while (expression);
Here is the sequence of steps of the while loop.

The order used is to complete step 1. Then to follow the sequence of step 2, step 3, and step 4. Once step 4 is finished, return to step 2 and repeat the sequence.
  step 1

  do {
    step 2
    step 3
  } while (step 4);
Here is an example code fragment of a do while loop that will print from zero to a user defined stopping point. Note, the statement body of the loop will execute one time, no matter if the value of i is greater than or equal to the value of stop.
  i = 0;

  do {
    System.out.println("i = " + i);
    i++;
  } while (i < stop);

for Loops

The generalized grammar for a for loop is as follows. First, you must begin with for, followed directly by a set of parenthesis. Within the parenthesis is a section for three different types of statements, an initialization statement, a conditional expression, and an increment statement listed in the order given. The initialization is separated from the conditional expression by a semi-colon. The conditional expression is separated from the increment by a semi-colon. The closing parenthesis is listed after the increment. Next, the for and parenthesis will be followed by a connected statement block, marked by the opening and closing pair of curly brackets. Example syntax is listed here.
Here is the Example structure of the for loop.
  for (initialization; expression; increment) {
    // the statements to execute each time the loop iterates
  }
Here is the sequence of steps of the for loop.

The order used is to complete step 1. Then to follow the sequence of step 2, step 3, and step 4. Once step 4 is finished, return to step 2 and repeat the sequence.
  for (step 1; step 2; step 4) {
    step 3;
  }
Here is an example code fragment of a for loop that will print from a user defined starting point back down to zero.
  for (i = start; i >= 0; i--) {
    System.out.println("i = " + i);
  }

for-each Loops

The for-each loop extends the usage of the base for loop to include a specific cycle for moving through the elements of a collection (list). First, the collection must be declared and then initialized. Next, the keyword for is listed, followed by a set of parenthesis. Within the parenthesis is the declaration of variable of the same type as the elements within the collection, a colon separator, and the name of the collection. For every iteration of the loop, the first element of the collection, then the second, the third, and so on is copied into the variable declared within the parenthesis. This variable is available for use within the statement block of the loop. Example syntax is listed here.
Here is the Example structure of the for-each loop.
  // declaration and initialization of a collection

  for (declaration : collection) {
    // the statements to execute each time the loop iterates, using the declaration
  }
Here is the sequence of steps of the for-each loop.

The order used is to complete step 1. Then to follow the sequence of step 2 and step 3. Once step 3 is finished, return to step 2 and repeat the sequence. Note, there is no step 4 since the increment to the next element is performed and controlled by the for-each loop itself.
  step 1

  for (step 2) {
    step 3;
  }
Here is an example code fragment of a for-each loop that will print all of the values from a collection.
  ArrayList <Integer> numbers = new ArrayList <Integer> ();
  // use the method *add* to add numbers to the list, for example:
  numbers.add(5);
  numbers.add(3);

  for (Integer number : numbers) {
    System.out.println("Number: " + number);
  }