Can you rewrite this snippet without goto
c++, goto, refactoring
Solution
insideloopy:
cnt++;
if ( current->hasChild() )
{
current = current->child();
goto insideloopy;
}
I love infinite loops.
while (true) {
cnt++;
if (!current->hasChild()) break;
current = current->child();
}
Of course you can do it in many other ways (see other answers). do while, put the check in the while, etc. In my solution, I wanted to map nearly to what you are doing (an infinite goto, unless break)
Problem
Guys, I have the following code that is inside a big while loop that iterates over a tree. This is as fast as I can get this routine but I have to use a goto. I am not fundamentally against goto but if I can avoid them I would like to. (I am not trying to start a flame war, please.) The constraints: - The `current=current->child()` is expensive (it's a `shared_ptr`) so I'd like to minimize the use of that operation at all cost. - After the operation `current` should be the last child it found. - `cnt` must count each child it encounters. - cnt++ will be replaced by some other operation (or several operations) and should only appear once :) the code: ``` insideloopy: cnt++; if ( current->hasChild() ) { current = current->child(); goto insideloopy; } ``` Edit: Sorry guys, originally forgot to mention cnt++ should only appear once. It will be some kind of operation on the node, and should thus only be there one time. I'm also trying to avoid making that another function call.