继续声明的替代方法

时间:2020-10-28 14:39:45

标签: c continue

我正在寻找一种替换此函数中的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;
}

1 个答案:

答案 0 :(得分:1)

可以通过反转条件并将其余代码放入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;
}
相关问题