-2

I want to break the inner loop and increase the value of x if I find something at Matrix[x][y] but if I break in the y for loop it will just increase the value at y loop and not in the x loop.

int matrix[5][5]; //let's assume there are values inside
for(int i = 0; i < 5; i++)
   {
     for(int j = 0; j<5;j++)
      {
        int radius = 1;
        for(int x = radius - 1; x <= radius + i; x ++)
         {
          for(int y = radius -1; y <= radius +j; y++)
            {
               if((x+i) >= 0 && (y+j)>=0)
                 {
      //for example I find the number 45 in the matrix, then the search radius should be increased by 1 and start it over again.
                  if(matrix[x][y] == 45)
                     {
                       radius++;
                       break; // but if i break here the x value wont be changed
                      }

                  }
             }
          }
      }
Vishaal Shankar
  • 1,648
  • 14
  • 26

1 Answers1

0

Of non-local exit mechanisms C++ provides throwing:

try {
    for(int x = 0; x < 10; ++x) {
        for(int y = 0; y < 3; ++y) {
            std::cout << x << ", " << y << std::endl;
            if(y == 1 && x == 3) throw true;
        }
    }
}
catch(bool) {}
std::cout << std::endl;

Additionally you can modify the outer loop's variable so the final condition holds:

for(int x = 0; x < 10; ++x) {
    for(int y = 0; y < 3; ++y) {
        std::cout << x << ", " << y << std::endl;
        if(y == 1 && x == 3) {
            x = 10;
            break;
        }
    }
}
std::cout << std::endl;

(Or, if you don't want to modify the variable itself, say, it's not local to the loop and you'll need its value afterwards, add one more flag to the loop condtion:

bool continueOuter{true};
for(int x = 0; continueOuter && x < 10; ++x) {
    for(int y = 0; y < 3; ++y) {
        std::cout << x << ", " << y << std::endl;
        if(y == 1 && x == 3) {
            continueOuter = false;
            break;
        }
    }
}

)

bipll
  • 11,747
  • 1
  • 18
  • 32
  • 1
    use of exceptions would be inappropriate for this case. Needing to increase the radius of the search is not an "exceptional circumstance". It's an expected part of the algorithm. – Richard Hodges Mar 15 '18 at 12:26
  • And where exactly do I use exceptions? – bipll Mar 15 '18 at 12:27
  • 2
    try/catch/throw involve exceptions whether or not you derive the actual exception's type from std::exception. `throw int(1)` throws an exception of type `int`. – Richard Hodges Mar 15 '18 at 12:29
  • Can you kindly clarify: by "throws an exception" do you mean it is actually an exception from something, or is it a colloquial term for a non-local exit? – bipll Mar 15 '18 at 13:10
  • 1
    see [expr.throw] in the c++ standard draft: "Evaluating a throw-expression with an operand throws an exception (18.1); the type of the exception object is determined by removing any top-level cv-qualifiers from the static type of the operand and adjusting the type from “array of T” or function type T to “pointer to T”" http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf – Richard Hodges Mar 15 '18 at 13:15