The case for (or against) 'continue'

J

Jorgen Grahn

How is [continue] different then [goto a label at the while] ?

In the same way that a correctly chosen foo_cast<T>() is different
from a C-style cast. It expresses what you want more clearly, and is
less likely to be accidentally broken by other changes.

/Jorgen
 
N

Noah Roberts

Jeff said:
In C, the label gives you a place to insert hooks that will always be
run before the function returns. It's the C analog of a finally-block.
The pattern is very common in BSD device drivers.

Well, I suppose that's a great case for "natural" being based on
environment. If you're in an environment where code like that's used a
lot, get used to it. I think scope guards and such are a better method
in C++ but in C I can see the necessity.
 
N

Noah Roberts

Ian said:
That's the argument I would have used. I've hardly ever seen continue
used in C or C++ and I still have to remind my self where the program
flow ends up.

I've used it on rare occasion in cases more like this:

while (?)
{
if (this iteration needs to be skipped) continue;

... lots of goings on ...
}

But to use it repeatedly in a loop like the original example seems
confusing. When the cases are on equal footing I would rather see if/else.

On the other hand, something like my code might deserve to be refactored
into a function that can do the check and return.
 
A

Alf P. Steinbach

* Alf P. Steinbach:

Thanks to all who responded -- I have one follow-up question, though.

But first, in summary, it seems that

* Most C++ programmers use 'continue' sparingly if at all, while some
few use it analogously to an early return.

* Where it's used best practice seems to be to regard it as signaling that
the loop invariant has been re-established and some progress made.

* Whether its use in that sense appears natural or not depends strongly on
earlier familiarity with that use. To some (reading left-margin 'else's?)
it appears to break the visible program flow, requiring reading more. To
those who use it it appears to make that flow more explicit, requiring
reading less.

* It's darned difficult to find any example where its use is clearly
advantagous, not very open to debate.

* Examples constructed to show 'continue' to be natural and of advantage,
invite an urge to re-factor, e.g. breaking the loop body up into individual
routines, thus, 'continue' might indicate the need for re-factoring.

I've probably missed one or two pertinent points, but.

In light of the three last points, what is a good textbook example of 'continue'
(I'm unable to come up with one)?


Cheers & TIA.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top