continue 语句的替代方法

Alternative for the continue statement

我正在寻找一种方法来替换此函数中的 continue 语句。房屋规则规定它们不能使用,但我很难实施不会导致其余代码无法正常运行的替代品。

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;
}

第一个 continue; 可以通过反转条件并将其余代码放在 if 语句中来替换。

第二个continue;可以简单地删除,因为后面没有要执行的代码。

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;
}