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.
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.
Continue is used to go to the next iteration of a loop. So something like this would make sense. Now you could use what ever conditions (yours is a==5 to break on), and whatever business logic you wanted (mine is a silly, contrived example).
StringBuilder sb = new StringBuilder();
for(String str : strings) {
sb.append(str);
if(str.length() == 0) continue; // next loop if empty
str = str.substring(1);
sb.append(str);
if(str.length() == 0) continue; // next loop if empty
str = str.substring(1);
sb.append(str);
if(str.length() == 0) continue; // next loop if empty
sb.append(str);
}
Videos
continue is kind of like goto. Are you familiar with break? It's easier to think about them in contrast:
breakterminates the loop (jumps to the code below it).continueterminates the rest of the processing of the code within the loop for the current iteration, but continues the loop.
A continue statement without a label will re-execute from the condition the innermost while or do loop, and from the update expression of the innermost for loop. It is often used to early-terminate a loop's processing and thereby avoid deeply-nested if statements. In the following example continue will get the next line, without processing the following statement in the loop.
while (getNext(line)) {
if (line.isEmpty() || line.isComment())
continue;
// More code here
}
With a label, continue will re-execute from the loop with the corresponding label, rather than the innermost loop. This can be used to escape deeply-nested loops, or simply for clarity.
Sometimes continue is also used as a placeholder in order to make an empty loop body more clear.
for (count = 0; foo.moreData(); count++)
continue;
The same statement without a label also exists in C and C++. The equivalent in Perl is next.
This type of control flow is not recommended, but if you so choose you can also use continue to simulate a limited form of goto. In the following example the continue will re-execute the empty for (;;) loop.
aLoopName: for (;;) {
// ...
while (someCondition)
// ...
if (otherCondition)
continue aLoopName;