How to stop three loopings for in C

Asked

Viewed 238 times

5

Rstou trying to implement the AI ordered search algorithm and I have reached a point where I need to stop all loopings if I have found the NODE searched.

Look what I did, but it doesn’t work. I know that in PHP I could use break 3 to finish the three iterations, but in C 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
  • 3

    Something tells me that someone will post a xkcd comic in the answer.

3 answers

7

I found this solution in stackoverflow in English, there are other.

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

end:
  • 2

    In this case the use of goto is acceptable, but I would like to warn you: if your code has critical sessions, you never must enter/exit through a goto.

  • Um, I didn’t know I could use Goto in C, I use this command to handle errors for a VBA output. So I set the label as output in the last loop and in its content I break; for(int i = 0; i < 1000; i++) {&#xA;&#xA; for(int j = 0; j < 1000; i++) {&#xA;&#xA; if(condition) {&#xA;&#xA; goto saida;&#xA;&#xA; }&#xA;&#xA;} &#xA;&#xA;&#xA;saida:&#xA;&#xA;break;&#xA; Correto?

  • 1

    @Luiz the "output" would be a way to exit the loop and continue the external code. I do not have much knowledge in C, just found this solution. You’d better use the solution you’ve already chosen.

  • 2

    In the OS you get an automatic ban if you include "C" and "Goto" on the same page :-P (joke, but even ask about C and goto generates mass downvotes).

5


The first answer that came to mind, I’m pretty sure there’s some more sophisticated way to do that.

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;
    }
}

Other than that, all I remember is goto, but the same has discouraged use in C.

  • Good, I’ll follow that way anyway, thank you.

4

By seeing a function with so many levels of identation one within another the first thing that comes to mind is: this function is doing too much.

In an ideal environment, each function should have a very specific and isolated functionality and rely on other more trivial functions to make its operation. For cases like yours it is worth reviewing the organization of the code and do some Refactoring to split the task into more than one function.


More directly answering the question: There is a magic operation capable of breaking 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);
  • This answer is the best in my opinion, it will only be a problem in cases the code is very complex and it is necessary to use many variables within the loops, which can make the header of the function look strangely large or you have to abuse the dynamic arguments of va_*.

  • These are all cases of poorly organized code, with functionalities not so well planned. Ai fits modularize and make more use of structs. Having so many arguments is a sign, not the problem itself. Treating symptoms is not healing.

  • I thought of that possibility, but I declared the function void.

  • 1

    @Cahe This is what structs and object orientation principles are for (which can be applied in C).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.