Get rid of goto statements from C / C ++ built-in logic

I want to get rid of this operator goto. Can anyone tell me the logic for the same. The code below is not the exact code that I want to change, but it will support my question. Please do not ask about the meaning of the code when commenting on this post, as this is just an example.

int result[i][j];
for (int i = 0; i<100; i++)
{
    for (j = 0; j<100; j++)
    {
        result[i][j] = j++;
        if (j == 80)
            goto break1;
    }
}
break1:
…;
+4
source share
7 answers

Put these loops in the function, give it the correct name and return;when it will be done. If it is complex enough to require two cycles, it deserves a name.

, , .

, . , , -, , , -, .

+4

.

  • return
  • "", , " " "EvilTeach".
  • ( ++) try-catch throw exception, . , . , , .
+2

boolean (-) for.

int result[i][j];
bool cont = 1;
for (int i =0;i<100;i++)
{
    for(j = 0;j<100;j++)
    {
        result[i][j] = j++;
        if(j == 80)
        {
            cont = 0;            
            break; 
        }
    }
    if (cont == 0)
        break;
}
break1;

(: ).

+1

, . , , :

bool break_loop = false;
for (int i = 0; i < 100; ++i) {
    for (int j = 0; j < 100; ++j) {
        if (j == 80) {
            break_loop = true;
            break;
        }
    }
    if (break_loop) break;
}
+1
int result[i][j];
for (int i = 0; i<100; i++)
{
    for (j = 0; j<100; j++)
    {
        result[i][j] = j++;
        if (j == 80)
        {
           i = 100;
           break;
        }
    }
}
0

()

for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
        /* work */
        if (j == 80) i = j = 100; // 100 makes both loops terminate
    }
}
0
source
int result[i][j];
for (int i = 0; i<100; i++)
{
    int j;
    for (j = 0; j<100; j++)
    {
        result[i][j] = j++;
        if (j == 80)break;
    }
    if(j == 80) break;
}

Know that this is an old question, but it can be changed simply

0
source

All Articles