Davospike
Davospike

Reputation: 91

Alternative for the continue statement

I'm looking for a way to replace the continue statement in this function. The houserules state that they can't be used but I'm having difficulty implementing a replacement that doesn't cause the rest of the code to function incorrectly.

bool neighCheck (int i, int j, int a[][COLLENGTH])
{
   bool neighbourOnFire;
   int x, y, neighX, neighY, curreNeigh;

   /* Bool set up to change after neighbours looped*/
   neighbourOnFire = false;
   /* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
   for (x = -1; x < 2; x++) {
      for (y = -1; y < 2; y++) {
         /* Disregards current (middle) cell*/
         if ((x == 0) && (y == 0)) {
            continue;
         }
         /* Get indexes of the neighbour we're looking at */
         neighX = i + x;
         neighY = j + y;
         /* Checks for edges*/
         if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
            && neighY < COLLENGTH) {
            /* Get the neighbour using the indexes above */
            curreNeigh = a[neighX][neighY];
            /* Test to see if the neighbour is burning */
            if (curreNeigh == fire) {
               neighbourOnFire = true;
               continue;
            }
         }
      }
   }
   return neighbourOnFire;
}

Upvotes: 0

Views: 547

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The first continue; can be replaced by inverting the condition and putting the rest code inside if statement.

The second continue; can be simply removed because there are no code to execute after that.

bool neighCheck (int i, int j, int a[][COLLENGTH])
{
   bool neighbourOnFire;
   int x, y, neighX, neighY, curreNeigh;

   /* Bool set up to change after neighbours looped*/
   neighbourOnFire = false;
   /* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
   for (x = -1; x < 2; x++) {
      for (y = -1; y < 2; y++) {
         /* Disregards current (middle) cell*/
         if (!((x == 0) && (y == 0))) {
            /* Get indexes of the neighbour we're looking at */
            neighX = i + x;
            neighY = j + y;
            /* Checks for edges*/
            if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH
               && neighY < COLLENGTH) {
               /* Get the neighbour using the indexes above */
               curreNeigh = a[neighX][neighY];
               /* Test to see if the neighbour is burning */
               if (curreNeigh == fire) {
                  neighbourOnFire = true;
               }
            }
         }
      }
   }
   return neighbourOnFire;
}

Upvotes: 1

Related Questions