There is always a better way than using gotos. I haven't used a goto since I stopped programming in FORTRAN, back when Methuselah was in nappies.
There isn't a simple formula for rewriting code to remove gotos, because it depends on the structure of the individual case. But if you can distill your code into a small block that illustrates the problem , we can have a look at helping restructure it. The code in your original post is not enough because it doesn't tell us whether each goto is going forwards or backwards or jumping in or out of loops or whatever.
Here's a hint to be going on with.
Beginners often use a goto to simulate a simple loop.
bool test; :loopStart; // Etc. etc. test = SomethingOrOther(); if(test) goto loopStart;
But the while loop is provided for this purpose.
bool test = true;
while(test)
{
// Etc. etc.
test = SomethingOrOther();
}
No comments:
Post a Comment