How to stop three loopings for in C

5

I've been trying to implement the Artificial Intelligence sorted search algorithm, and I've come to the point that I need to stop all looping if I've found the searched node.

Look at the passage I made, but it does not work. I know that in PHP I could use break 3 to finalize the three iterations, but in C it does not work or found on the internet:

for(int i=0;i<N;i++) {
   for(int j=0;j<N;j++){
      if(entra){
         soma = 0;
         custo = 99999;
         for(int k=0;k<N;k++){
            if (abertos[k] > 0){
                //se o nó estiver em fechados, pula para próxima iteração.
                if (fechados[k]==1){
                    continue;
                }
                if(mCusto[k] < custo){
                    custo = mCusto[k];
                    posatual = k;
                }

                if (posatual+1 == dest){ //Se chegou ao destino então para o looping.
                    printf("Sucesso\n\n");
                    break;
                    break;
                    break;
                }
            }
            soma += abertos[k];
            printf("\nsoma = %d\n",soma);
            fechados[posatual] = 1; //coloca o nó escolhido em fechados.
        } //fim laço k
    
asked by anonymous 09.04.2014 / 07:08

3 answers

5

The first response that came to mind, I'm pretty sure it has some more sophisticated way of doing this.

int i, j, k;
int stop = 0;

for(i = 0; i < 10; ++i) {
    for(j = 0; j < 10; ++j) {
        for(k = 0; k < 10; ++k) {
            if(/* Alguma condição aleatória*/) {
                stop = 1;
                break;
            }
        }
        if(stop) {
            break;
        }
    }
    if(stop) {
        break;
    }
}

Out of this, I only remember goto , but it has discouraged use in C.

    
09.04.2014 / 07:39
7

I found this solution in the stackoverflow in English, there are others.

for(int i = 0; i < 1000; i++) {
    for(int j = 0; j < 1000; i++) {
        if(condition) {
            goto end;
    }
} 

end:
    
09.04.2014 / 07:42
4

When you see a function with so many indentation levels one inside the other, the first thing that comes in mind is: this function is doing too much .

Ideally, each function must have a very specific and isolated functionality and rely on other, more trivial functions to make its operation. For cases like yours it's worth reviewing the code organization and doing some refactoring to split the task into more than one function.

Answering the question more directly: There is a magic operation that can break as many levels as needed: return . If the goal is to search for some node, you can do so:

int findNode(int a, int b, int c, int d, .../*stuff you need*/) {
    for (i = 0; i < 10; ++i) {
        for (j = 0; j < 10; ++j) {
            for (k = 0; k < 10; ++k) {
                if (/*algo*/)
                    return a+b*c-d;
            }
        }
    }
}

// Mais para frente...
int dest = findNode(34, 64, aaa, y+f);
fazerAlgoCom(dest);
    
09.04.2014 / 15:05