Standards-9
Loops


Major errors

Only one loop per function [30 points each, 6% max]

Each function definition can only have one loop.

It must be possible to do a loop's body more than once [10 points each, 3% max]

Do not use while to mean if. If you write a loop, there should exist some circumstances where the loop body can be done more than once.

Do not mix loops and recursion [10 points each, 3% max]

A function that contains a loop must not also use recursion.

Do not emulate two nested loops or two consecutive loops using a single loop. [40 points each, 7% max]

It is possible to do a lot in a single loop by managing variables in complicated ways. Imagine that you want to do two nested for-loops, as follows.
  for(int i = 0; i < n; i++)
  {
    for(int j = 0; j < m; j++)
    {
       doSomething(i,j);
    }
  }
But that uses two loops, so you emulate it with one loop, as follows.
  int i = 0;
  int j = 0;
  while(i < n)
  {
    doSomething(i,j);
    j++;
    if(j == m)
    {
      j = 0;
      i++;
    }
  }
You can recognize this by the reset of j to 0 when j = m.

That is not an acceptable way to avoid two loops in one function. Just write a function to handle the inner loop.

  void doAll(int i)
  {
    for(int j = 0; j < m; j++)
    {
       doSomething(i,j);
    }
  }
Now your loop becomes
  for(int i = 0; i < n; i++)
  {
    doAll(i);
  }


The body of a loop must not be a semicolon [4 points each, 2% max]

Never write
  while(condition);
Do not use a bare semicolon as the body of any type of loop.


Minor errors

Code that is done at the end of the loop should be written after the loop. [4 points each, 2% max]

Look at the following example.
  int loopdemo(int n)
  {
    for(int i = 0; i < n; i++)
    {
      doSomething(i);
      if(i == n-1)
      {
        finish(i+1);
      }
    }
  }
Notice that statement finish(i+1) is only done at the end of the last iteration of the loop. This function should be written as follows.
  int loopdemo(int n)
  {
    for(int i = 0; i < n; i++)
    {
      doSomething(i);
    }
    finish(n);
  }

Do not change the value of a for-loop control variable in the loop body [6 points each, 3% max]

A for-loop control variable (any variable that is modified in the third part of a for-loop heading) should only be modified in the for-loop heading, not in the body of the loop. For example, do not write
  for(i = 1; i < n; i++) 
  {
    ...
    i = n;
    ...
  }
If you want to break out of a loop, use break or return.

Do not change a for-loop end value in the loop without justification [6 points each, 3% max]

Do not change the end-value of a for-loop just as a way to get out of the loop. For example,
  for(i = 1; i < n; i++) 
  {
    ...
    if(condition)
    {
      n = 0;
    }
    ...
  }
is not a good way to cause the loop to end. If you want to exit a for-loop, use break or return.

Initialize for a for-loop in the heading where sensible [4 points each, 1% max]

The first component of a for-loop heading is for initializing the control variable. Use it unless you have a good reason for not using it.

Where sensible, a for-loop heading should control one variable [5 points each, 2% max]

Loop
    for(int k = 2; k <= n; p = p * x)
    {
      k++;
    }
does some of the control of k in the loop heading and some in the body. Instead, put all of the control of k in the loop heading.
    for(int k = 2; k <= n; k++)
    {
      p = p * x;
    }