I'm trying to skip part of my loops by using continue; however, I am unsure where how much code each continue statement skips. My suspicion is that, in the below code, the continue statements effectively do nothing and put my code right before the return false statements. I have marked with big HEREs where I would instead like the continue statements to take the code. I am curious if anyone has ideas about how to rewrite this code to make it work properly. Sure, the shunned goto statement would always work, but is there a more legitimate method?

for (int n=0; n <= 8; n++)
     {
          if  (Board [row][column] == Board [n][column])
          {
               if (n == row)
               {
                    continue;
               }
               return false;
          }
HERE
          if (Board [row][column] == Board [row][n]) 
          {
               if (n == column)
               {
                    continue;
               }
               return false;
          }
HERE
     }
     return true;

My suspicion is that, in the below code, the continue statements effectively does nothing and put my code right before the return false statements.

Correct :)

I am curious if anyone has ideas about how to rewrite this code to make it work properly.

I would like to help, but I haven't got a clue what this code is suppose to do. Perhaps a bit explanation?

I can give you a few example of how to break out of a loop

for (int i=0; i<5; i++)
{
      std::cout << i << "\n";
      if (i==3) break;
}

output:

0
1
2
3

Or you could use a flag to check if you want to break:

int i = 0;
bool brk = false;
while (i < 10 && !brk)
{
    std::cout << i << "\n";
    if (i==3) brk = true;
    i++;
}

Same output as before.

I think you could use the following

for (int n=0; n <= 8; n++)
{
    if  (n != row && Board [row][column] == Board [n][column])
    {
        return false;
    }

    if (n != column && Board [row][column] == Board [row][n]) 
    {
        return false;
    }
}
return true;

well...how ridiculously simple compared to my solution. Take a look at what I did.

bool Verify(int row, int column)
{
     bool statusR = true;
     bool statusC = true;
     for (int n=0; n <= 8; n++)
     {
          if  (Board [row][column] == Board [n][column])
          {
               statusR = false;
               if (n == row)
               {
                    statusR = true;
               }
          }
          if (Board [row][column] == Board [row][n]) 
          {
               statusC = false;
               if (n == column)
               {
                    statusC = true;
               }
          }
          if (statusR == false || statusC == false)
          {
          return false;
          }
     }
     return true;
}

Thank you so much for the help guys.

My suspicion is that, in the below code, the continue statements effectively do nothing and put my code right before the return false statements.

No. "continue" skips the remaining iteration of the loop (in this case, the for loop). So it goes directly to the "n++" step, and then starts the next iteration of the loop, testing the condition and executing the code. So it is not what you want (at least for the first big if block, "continue" would skip the second big if block entirely).

well...how ridiculously simple compared to my solution. Take a look at what I did.

bool Verify(int row, int column)
{
     bool statusR = true;
     bool statusC = true;
     for (int n=0; n <= 8; n++)
     {
          if  (Board [row][column] == Board [n][column])
          {
               statusR = false;
               if (n == row)
               {
                    statusR = true;
               }
          }
          if (Board [row][column] == Board [row][n]) 
          {
               statusC = false;
               if (n == column)
               {
                    statusC = true;
               }
          }
          if (statusR == false || statusC == false)
          {
          return false;
          }
     }
     return true;
}

Thank you so much for the help guys.

This code does not do the same thing as what you said you wanted to do. If statusC is false after the first big if block, it does not immediately return, and instead goes through the second big if block, which could potentially set it true.

This article has been dead for over six months. Start a new discussion instead.