[quote name='NightCreature83' timestamp='1354278416' post='5005667']
how would you replace the goto in this bit with an exception
for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
goto loopBreak;
}
}
}
loopBreak:
printf("%d", 100);
Sorry, been away and a tad late to the party.
There was a great example in Bjarne Stroustrup's book demonstrating throwing an exception out of a deeply nested loop containing the result and then catching it after.
This was however a toy implementation and isn't recommended at all.
I have no problem using a goto in this way (and I do find myself doing similar). In my opinion, this
isnt misusing C.
[/quote]
Just thought I'd throw an alternative out here (it's kind of hard to give any kind of "proper" alternative, because the original will always print 100, no matter what...):
int findSomething()
{
for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
return 100;
}
}
}
return -1;
}
// Then in your code...
printf("%d", findSomething());
I've found that most of the time, deeply nested stuff can be turned into useful, smaller functions. Not always, but a lot of the time.