Control Structures: Loops

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--) {
    printf("i = %d\n", i);
  }

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 1 (true) or 0 (false) result or will simply be the literal value 1 or 0. 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) {
    printf("i = %d\n", 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 1 (true) or 0 (false) result or will simply be the literal 1 or 0. 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 {
    printf("i = %d\n", i);
    i++;
  } while (i < stop);