theWalker
theWalker

Reputation: 2020

C multi-loop for() basics

How to write example code without goto and without additional procedure call?

for(i=0;i<imax;i++)
{   for(j=0;j<jmax(i);j++)
    {   for(c=0;c<cmax(j);c++)
        {   if(!check1(c))
            {    if(check2()) goto ni;
                 else goto nj;
            }
        }
    // **EDIT** procedure call is here
nj:;
    }
ni:;
}

Upvotes: 1

Views: 1320

Answers (2)

Farhad
Farhad

Reputation: 4181

check this code:

    bool flag = false;
    for(i=0;i<imax;i++)
    {   for(j=0;j<jmax(i);j++)
        {   for(c=0;c<cmax(j);c++)
            {   if(!check1(c))
                {    if(check2()) {flag=true;break;}// goes to ni
                     else break; //goes to nj
                }
            }
            if(flag){flag=false; break;}
            nj:;
        }
        // procedure
    ni:;
    }

Upvotes: 2

BlindDriver
BlindDriver

Reputation: 681

goto nj can be replaced with break. however, break can't help you if you need to break out of nested loops. I think this is a perfectly legit use case for goto. The only alternative I can think of is to sett the loop counter outside of its range, e.g. c = cmax(j). Worse than goto, in my opinion.

Upvotes: 2

Related Questions