G
G
gleendo2018-11-07 14:09:33
C++ / C#
gleendo, 2018-11-07 14:09:33

How to exit multiple nested loops?

Please tell me how can I get out of 2+ nested loops? If at the very bottom the condition is triggered, then complete the work of all cycles at once? without goto
PS. C++/Py

for (int i = 0; i < LIMIT; i++)
  {
    for (int j = 0; j < LIMIT; j++)
    {
      int value = matrix[i][j];

      for (int a = 0; a < LIMIT; a++)
      {
        for (int b = 0; b < LIMIT; b++)
        {
          if (value == matrix[a][b])
            count++;

          if (count == 2)
          {
            std::cout << "Indexes of elements: " << "[" << i << ", " << j << "], ";
            std::cout << "[" << a << ", " << b << "]" << std::endl;

            // exit
          }
        }
      }

      count = 0;
    }
  }

Answer the question

In order to leave comments, you need to log in

6 answer(s)
1
15432, 2018-11-07
@15432

i = j = a = LIMIT;
break;

P
Pavel, 2018-11-07
Yazovskikh @unepic

iamevg , this is not an answer to the question about exactly "how to get out of a dozen nested loops", it's just another solution to your problem that does not require this. More divisions, fewer comparisons:

std::vector<std::vector<int>> matrix = {{1,2,3,42},{5,6,7,8},{9,10,11,42}};
    const size_t limit = matrix.size() * matrix.front().size();
    const size_t cols = matrix.front().size();
    size_t i = 0;
    size_t j = 1;
    while(i < limit - 1 && matrix[i / cols][i % cols] != matrix[j / cols][j % cols])
    {
        if(++j == limit) j = ++i + 1;
    }
    if(i < limit - 1)
    {
        std::cout << "Indices of elements: [" << i / cols << "," << i % cols << "]";
        std::cout << "[" << j / cols << "," << j % cols << "]" << std::endl;
    }
    else std::cout << "Not found" << std::endl;

K
ks0, 2018-11-07
@ks0

And now the task is to arrange it following the MISRA C standard - without break, without goto, without modifying the loop counter in the loop body, with one exit point from the function =)

D
devalone, 2018-11-07
@devalone

Put it in a function that looks for what you need and returns as a result, like this:

/* looks for some indices */
std::pair<size_t, size_t> findSomething(/* some parameters */) {
// ...
  return value;
// ...
}
// ...
auto result = findSomething(/* some arguments */);
std::cout << "[" << << result.first << ", " << result.second << "]" << std::endl;

A
Ariox41, 2018-11-08
@Ariox41

The simplest way for c++ is to wrap it in a lambda and exit with return. A similar approach can be used to initialize variables, but the readability of such code is still debated.

LIMIT = ...
matrix = ...
count = ...
[&]{
    for (int i = 0; i < LIMIT; i++) {
        for (int j = 0; j < LIMIT; j++){
            int value = matrix[i][j];

            for (int a = 0; a < LIMIT; a++) {
                for (int b = 0; b < LIMIT; b++)  {
                    if (value == matrix[a][b]){
                        count++;
                    }
                    if (count == 2) {
                        std::cout << "Indexes of elements: " << "[" << i << ", " << j << "], ";
                        std::cout << "[" << a << ", " << b << "]" << std::endl;

                        return;
                    }
                }
            }
        count = 0;
        }
    }
}();

D
d_ilyich, 2018-11-08
@d_ilyich

In this particular case, you can "expand" the matrix into a one-dimensional array and get by with one cycle. Here, I jotted it down in haste.

#define MAX_SAME 2
...
void search_matrix( int* matr, const int limit )
{
  int same_count = 0;
  int i = 0;
  int j = i + 1;

  while ( same_count < MAX_SAME && i < limit*limit - 1 ) {
    if ( matr[ i ] == matr[ j ] ) {
      same_count++;
    }

    if ( same_count < MAX_SAME ) {
      j++;
      if ( j == limit*limit ) {
        i++;
        same_count = 0;
        j = i + 1;
      }
    }
  }

  if ( same_count == MAX_SAME ) {
    cout << same_count << ' ' << i << ' ' << j;
  }
}

Yes, it is necessary to "return" indexes to two-dimensional ones.
PS I'm not strong in C++, if anything - don't kick too much :)
PPS There are, of course, 2 cycles. I'm sorry, I was excited :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question