Using break in nested for loop

This is my RFID reading device project.I’m trying to match read UID and saved UIDs.
UID has 4 bytes.
I used a 2 diamensional array for store my UIDs,

byte savedUIDs[][keySize] = {
  {12, 123, 0, 0},
  {22, 20, 11, 33},
  {19, 253, 216, 0},//correct white card
  {13, 253, 216, 0}
};

I used following loop system for match my UID byte with saved byte.

  for (int x = 0; x < numberOfUIDs ; x++) {
    Serial.print("x : ");
    Serial.println(x);
    for (int i = 0; i < keySize; i++ ) {
      Serial.print("i : ");
       Serial.println(i);
      if (mfrc522.uid.uidByte[i] != savedUIDs[x][i]) {
        access = false;
        break;
      }
      else {
        access = true;
        
      }
    }
    if (access) {
      Serial.println("Access OK");
      Serial.print("Success Location Index : ");
      Serial.println(x);
      break;
    }
    else {
      Serial.println("Access Failed");
      break;
    }

  }

if second for loop(nested loop) execute break,first loop also exited. :confused:
But,I want to run first loop.
If I put correct UID as first element I can print “Access OK”.
I want to scan all of UIDs until meet the correct one.
How can I do this?

If a byte does not match in the inner loop then you set match to false and break out of the inner loop. Then in the outer loop you break if access is false thus terminating the for loop. Do you need to set access to false in the inner loop ?

How can I do this?

 bool foundMatch = false;
  for (int x = 0; x < numberOfUIDs && !foundMatch; x++)
  {
    Serial.print("x : ");
    Serial.println(x);
    for (int i = 0; i < keySize; i++ )
    {
      Serial.print("i : ");
      Serial.println(i);
      if (mfrc522.uid.uidByte[i] != savedUIDs[x][i])
      {
        access = false;
        break;
      }
      else
      {
        access = true;  
      }
    }
    if (access)
    {
      Serial.println("Access OK");
      Serial.print("Success Location Index : ");
      Serial.println(x);
      foundMatch = true;
    }
    else
    {
      Serial.println("Access Failed");
    }
  }

All that white space in your code, with thecurlybracesonthesamelineasthestatement,jammedtightupagainsttheendofthestatement looks stupid.

Try this :

 for (int x = 0; x < numberOfUIDs ; x++) {
    Serial.print("x : ");
    Serial.println(x);
    access = true;
    for (int i = 0; i < keySize; i++ ) {
      Serial.print("i : ");
       Serial.println(i);
      if (mfrc522.uid.uidByte[i] != savedUIDs[x][i]) {
        access = false;
      }
   } //Do your "access == true" stuff here
 }

You don’t need to break out of the for loop, as nowhere in it is access set to true.
One or one hundred false readings will set access to false. Only a set of correct readings will leave access as true.

UKHeliBob:
If a byte does not match in the inner loop then you set match to false and break out of the inner loop. Then in the outer loop you break if access is false thus terminating the for loop. Do you need to set access to false in the inner loop ?

yes,I want to add break in to inner loop.If not,my program processing time will go so high.

The problem is not the break in the inner loop, it is the break in the outer loop. Why do you break out of the outer loop when you find a mismatch in the inner loop ?

   else {
      Serial.println("Access Failed");
      break;

is in the outer loop. To me it says “one of the tests failed so stop doing any tests”. Why stop testing the UIDs if one of them does not match ?

.If not,my program processing time will go so high.

Have you any idea how fast the two nested for loops will run ? I strongly suspect that in practice you would not notice the difference even if both were allowed to run to completion. Have you tried it ?

You are correct @UKHeliBob and Henry_Best…

I cleared my problem.It’s my lack of logical thinking… :frowning:
Thanks for help @UKHeliBob,@PaulS and @Henry_Best.

  //UID scaning loop
  for (int x = 0; x < numberOfUIDs && !access ; x++) {
    Serial.print("x : ");
    Serial.println(x);
    for (int i = 0; i < keySize; i++ ) {
      Serial.print("i : ");
      Serial.println(i);
      if (mfrc522.uid.uidByte[i] != savedUIDs[x][i]) {
        access = false;
        break;
      }
      else {
        access = true;
        successLoc = x;
      }
    }
  }
  //end of UID scaning loop

  if (access) {
    Serial.println("Access OK"); //LCD print
    Serial.print("Success Location Index : ");
    Serial.println(successLoc); //2^successLoc = LEDLoc(LED location of the board)
    access = false;
 
  }
  else {
    Serial.println("Access Failed");
  }