I'm new to C++. I have the following form of code. I was wondering how I could rewrite this without the use of goto. If I use break in place of it, then I still check the if statement following the loop on every pass through this code even though if I did break I know this condition will fail, so why check it? The only solution I was able to come up with is to make this code a function and replace the goto with a return and remove the if conditional following the loop, leaving just the statements. Would this be a place where goto IS acceptable?
Edit: I should also mention the reason I leave the loop when we find an equal member, is because I don't need to check the remainder of the members in my object cause I already know the one we found is unique to the objects we are iterating, therefore there will never be a match if we continued through the rest of the iterators. So I just exit the loop then.
while (begIt != endIt)
if ((*begIt).member == someObject.member){
// Do these statements
goto someLabel; // then goto someLabel
}
++begIt;
}
if (begIt == endIt){ // We must have not found an equal member
// So do these statements
}
someLabel: // ...