Java continue at the end of if

John Frost picture John Frost · Nov 9, 2012 · Viewed 51.8k times · Source

I have some example code from a book and the author is always using an continue at the end of an if.

Example:

int a = 5;
if(a == 5)
{
// some code
continue;
}

Now for me this doesn't make any sense. Might there be some kind of quality management reasoning behind it or am I just missing some bigger point?

Answer

Óscar López picture Óscar López · Nov 9, 2012

Maybe that snippet of code was inside a loop (for/while/do...while)? otherwise it does not make any sense to put a continue inside a conditional statement.

As a matter of fact, an orphaned continue (e.g.: one that is not nested somewhere inside a loop statement) will produce a continue cannot be used outside of a loop error at compile time.