Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I avoid checking everything with a different If using for loops?

In CPP, how do I avoid walls of ifs using for loops?
stackoverflow is requesting more text soo…
Code Example:

    if(masiv[0][0] == 'X'){
        masiv[0][0] = '1';
    }
    if(masiv[0][1] == 'X'){
        masiv[0][1] = '2';
    }
    if(masiv[0][2] == 'X'){
        masiv[0][2] = '3';
    }
    if(masiv[1][0] == 'X'){
        masiv[1][0] = '4';
    }
    if(masiv[1][1] == 'X'){
        masiv[1][1] = '5';
    }
    if(masiv[1][2] == 'X'){
        masiv[1][2] = '6';
    }
    if(masiv[2][0] == 'X'){
        masiv[2][0] = '7';
    }
    if(masiv[2][1] == 'X'){
        masiv[2][1] = '8';
    }
    if(masiv[2][2] == 'X'){
        masiv[2][2] = '9';
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (int y = 0; y < 3; ++y) {
  for (int x = 0; x < 3; ++x) {
    if (masiv[y][x] == 'X') {
      masiv[y][x] = '1' + 3 * y + x;
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading