Answer to Question while-4

Nested loop simulation is using one loop to simulate two or more nested loops. Typically, it has two control variables (say, i and j) that are managed in the loop body so that the values of i and j are something like this.
  i  j
  1  1
  1  2
  1  3
  1  4
  2  1
  2  2
  2  3
  2  4
  3  1
  …
You don't use it because it is difficult for a reader to understand. It is better to use two nested loops. Instead of
  int i = 0;
  int j = 0;
  while(i < n)
  {
    doSomething(i,j);
    j++;
    if(j == m)
    {
      j = 0;
      i++;
    }
  }
you would write
  for(int i = 0; i < n; i++)
  {
    for(int j = 0; j < m; j++)
    {
       doSomething(i,j);
    }
  }
The latter form is much more clear.

Since you are not allowed to put two loops in the body of a function, break out the inner loop into a separate function.

  void dorow(int i, int m)
  {
    for(int j = 0; j < m; j++)
    {
       doSomething(i,j);
    }
  }

  …
  for(int i = 0; i < n; i++)
  {
    dorow(i, m);
  }
Note how obvious that transformation is when the two nested loops are shown in a sensible way.