views:

2463

answers:

6

I saw this keyword for the first time and I was wondering if some one could explain to me what it does. The situation in which I saw the keyword was:

if(obj.isFlagSet())
     ;
else
     continue;

I can't seem to figure out what the continue is doing.

+20  A: 

A continue statement without a label will re-execute from the condition the innermost while or do, or loop, and from the update expression 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 execute in the same way the correspondingly labeled loop. This can be used to escape deeply-nested loops, or simply for clarity. If you're truly perverse you can also use it to simulate a limited form of goto. In the following example the continue will re-execute the for (;;) loop.

aLoopName: for (;;) {
  // ...
  while (someCondition)
  // ...
    if (otherCondition)
      continue aLoopName;

Sometimes continue is also used as a placeholder in order to make an empty loop body clearer.

for (count = 0; foo.moreData(); count++)
  continue;

The same statement without a label also exists in C and C++. In Perl it's named next.

Diomidis Spinellis
It's worth noting that for loops execute the update code before re-evaluating the condition after a continue.
Dave L.
Thanks, I updated the answer accordingly.
Diomidis Spinellis
+15  A: 

continue is kind of like goto. Are you familiar with break? It's easier to think about them in contrast:

break terminates the loop (jumps to the code below it)

continue terminates the rest of the processing of the code within the loop for the current iteration, but continues the loop.

Dustin
Oh I see thank you. I didn't realize the fact it was in loop was important.
faceless1_14
good explanation!
mmattax
i like that your explanation got to the point quickly and effectively
Carlos Rendon
This makes more sense than the selected answer.
eleven81
+2  A: 

Let's see an example

int sum = 0;
for(int i = 1; i <= 100 ; i++){
    if(i % 2 == 0)
         continue;
    sum += i;
}

This would get the sum of only odd numbers from 1 to 100

m3rLinEz
+1  A: 

If you think of the body of a loop as a subroutine, "continue" is sort of like "return". The same keyword exists in C, and serves the same purpose. Here's a contrived example:

for(int i=0; i < 10; ++i) {
  if (i % 2 == 0) {
    continue;
  }
  System.out.println(i);
}

This will print out only the odd numbers.

Vineet
A: 

Generally, I see "continue" (and "break") as a warning that the code might use some refactoring, especially if the 'while' or 'for' loop declaration isn't immediately in sight. The same is true for 'return' in the middle of a method, but for a slightly different reason.

As others have already said, "continue" moves along to the next iteration of the loop, while "break" moves out of the enclosing loop.

These can be maintenance timebombs because there is no immediate link between the "continue"/"break" and the loop it is continuing/breaking other than context; add an inner loop or move the "guts" of the loop into a separate method and you have a hidden effect of the continue/break failing.

IMHO, it's best to use them as a measure of last resort, and then to make sure their use is grouped together tightly at the start or end of the loop so that the next developer can see the "bounds" of the loop in one screen.

"continue", "break", and "return" (other than the One True Return at the end of your method) all fall into the general category of "hidden GOTOs". They place loop and function control in unexpected places, which then eventually causes bugs.

Tom Dibble
One True Return? That can make for some unreadable code. Check out the question at http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement
Rontologist
A: 

As Tom already pointed out, it is our wellknown old wolf-friend, the GOTO in a sheep's clothing !

Just kidding.

blabla999