11

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?

John Frost
  • 673
  • 1
  • 10
  • 24

4 Answers4

25

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
5

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);
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
2

Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop, but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop's end. The continue statement performs such an action. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression. For all three loops, any intermediate code is bypassed. Here is an example program that uses continue to cause two numbers to be printed on each line: // Demonstrate continue.

class Continue
{ 
public static void main(String args[]) 
    { 
    for(int i=0; i<10; i++) 
        { 
    System.out.print(i + " "); 
    if (i%2 == 0)
    continue; 
    System.out.println(""); 
        } 
    } 
}

This code uses the % operator to check if i is even. If it is, the loop continues without printing a newline. Here is the output from this program:

0 1
2 3
4 5
6 7
8 9

Abhishekkumar
  • 1,102
  • 8
  • 24
1

Normally, the continue is used to escape deeply-nested loops, or simply for clarity (debatable).

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

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

It a matter of taste i personally don't use continue.

Community
  • 1
  • 1
dreamcrash
  • 47,137
  • 25
  • 94
  • 117