bobobobo
bobobobo

Reputation: 67224

Clean way to break outer for loop from inner for loop

Say you have a double-nested for, like when you are trying to see if an item in one unsorted array is inside another unsorted array. So for example say you have 2 lists candidates and corruptPeople, you're going through the candidates and skipping those that are listed in corruptPeople.

for( int i = 0 ; i < lenCandidates ; i++ )
{
    // veto candidates[i] if candidates[i] is in corruptPeople at all
    for( int j = 0 ; j < lenCorruptPeople ; j++ )
        if( candidates[i] == corruptPeople[j] )
             break_this_loop_and_then_continue_outer_loop ;

    // initiate the eligible candidates
    initiate( candidates[i] ) ;
}

Upvotes: 1

Views: 154

Answers (4)

Steve Jessop
Steve Jessop

Reputation: 279255

Moving the inner loop to another function often helps:

typedef int Person; // or whatever it is.

Person *findPerson(Person *first, Person *last, Person val)
{
    while (first != last)
    {
        if (val == *first) return first;
        ++first;
    }
    return 0;
}

...

for( int i = 0 ; i < lenCandidates ; i++ )
{
    // veto candidates[i] if candidates[i] is in corruptPeople at all
    if (!findPerson(corruptPeople, corruptPeople+lenCorruptPeople, candidates[i]))
    {
        // initiate the eligible candidates
        initiate( candidates[i] ) ;
    }
}

Upvotes: 0

Jay
Jay

Reputation: 24895

How about the below code? Am I missing something?

for( int i = 0 ; i < lenCandidates ; i++ )
{
    // veto candidates[i] if candidates[i] is in corruptPeople at all
    int j;
    for( j = 0 ; j < lenCorruptPeople ; j++ )
    {
        if( candidates[i] == corruptPeople[j] )
          break;
    }

    //j will be equal to lenCorruptPeople only when candidate is not in corrupt list   
    if(j==lenCorruptPeople)
    {
        initiate( candidates[i] ) ;
    }

}

Upvotes: 0

goji
goji

Reputation: 7092

Much better to use an additional variable than to resort to a goto:

for( int i = 0 ; i < lenCandidates ; i++ )
{
    // veto candidates[i] if candidates[i] is in corruptPeople at all
    int corrupt = 0;
    for( int j = 0 ; j < lenCorruptPeople ; j++ )
        if( candidates[i] == corruptPeople[j] )
        {
            corrupt = 1;
            break;
        }

    // initiate the eligible candidates
    if (!corrupt) initiate( candidates[i] ) ;
}

Upvotes: 2

bobobobo
bobobobo

Reputation: 67224

One way to do this (which I'm open to peer review for!) is using goto:

for( int i = 0 ; i < lenCandidates ; i++ )
{
    // veto candidates[i] if candidates[i] is in corruptPeople at all
    for( int j = 0 ; j < lenCorruptPeople ; j++ )
        if( candidates[i] == corruptPeople[j] )
             goto END_FOR ;

    // initiate the eligible candidates
    initiate( candidates[i] ) ;

    END_FOR:
    ; // seem to need an empty statement to make it compile
}

I'm curious what others have to say of the use of goto in this context. A dogmatic disagreeal with goto means you're going to have a dogmatic application of structured programming.. which looked pretty bad when I tried it.

Upvotes: 3

Related Questions