What Is the "Continue" Keyword and How Does It Work in Java

What is the continue keyword and how does it work in Java?

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;

How does continue work?

The basic for statement structure is as follows:


BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

Now, from JLS §14.14.1.3. Abrupt Completion of for Statement:

If execution of the Statement completes abruptly because of a continue with no label, then the following two steps are performed in sequence:

  1. First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate part is not present, no action is taken.

  2. Second, another for iteration step is performed.

If execution of the Statement completes abruptly because of a continue with label L, then there is a choice:

  • If the for statement has label L, then the following two steps are performed in sequence:

    1. First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate is not present, no action is taken.

    2. Second, another for iteration step is performed.

  • If the for statement does not have label L, the for statement completes abruptly because of a continue with label L.

(emphasis mine)

ForUpdate, in your case, is i++. Based on what's above:

  • Your first snippet falls under the first case, so i is incremented.

  • Your second snippet falls under the second case, so i is not incremented because the for statement completes abruptly.

Note that if you had continue LABEL1 in your second snippet, i would have been incremented as in your first snippet (in accordance with the JLS).

As a tip for the future, for definitive answers regarding language rules/semantics, you should always consult the language specification. For Java, that's the JLS.

Explain about java continue & break the following code briefly

The following line first checks if 0 < 5 and then increases the value of i to 1:

while (i++ < 5)

Thus the following statement prints 1 0 as the value of i is now 1 and that of j is 0:

System.out.print(i + " " + j + " ");

As a result of the two above mentioned processings, the following line is evaluated as switch (1 + 0) and then the value of j is increased to 1:

switch (i + j++)

As a result of the above mentioned processing, case (1) becomes true and therefore the control goes to label2 and again falls back to the following statement which prints 1 1:

System.out.print(i + " " + j + " ");

Now, the following line is evaluated as switch (1 + 1) and then the value of j is increased to 2:

switch (i + j++)

As a result of the above-mentioned processing, case (2) becomes true and therefore the control goes to label1 from where it falls to the following line which first checks if 1 < 5 and then increases the value of i to 2:

while (i++ < 5)

Then it goes to the following statement which prints 2 2 as the value of i is now 2 and that of j is 2:

System.out.print(i + " " + j + " ");

And so on...

I hope, it is clear to you. Apart from this, I also recommend you follow Java naming convention e.g. class example should be class Example as per the naming convention.

Feel free to comment in case of any doubt/issue.

Difference between break and continue statement

break leaves a loop, continue jumps to the next iteration.

Java continue at the end of if

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.

Why is it necessary to use break and continue statements in Java while loops?

Why is it Necessary To Use “break” and “continue” Statements In Java
While Loops?

It doesn't.

You need to use break and continue only if your statements are not enough to cover the logic that you want to apply.

It doesn't mean that it is necessarily bad but sometimes, it is overused and the code could be simpler without it.

Look at your code for example.

1)The continue is helpless.

After the if statement, the loop goes on. It is exactly what does continue.
It would make sense if you had some statements after the else statement and you would not execute but it is not the case.

2) The break could also be removed.

You break because the while condition doesn't take into consideration end of loop.

While x < 21, x is incremented but as x keeps this value after, so
while (x <= 21){ will always be true.

So you have to find a way to exit from the block while to avoid an infinite loop.

You do so a break in the else.

You can write the same logic without break if the while condition handles the exit condition.

You can do it by introducing a boolean variable that provides a natural way to exit the loop when the expected age is reached :

int x = 0;
boolean isAgeReached = false;

while (!isAgeReached) {
if (x < 21) {
System.out.println("You cannot drink because you are only " + x + " years old.");
x++;

}
else {
System.out.println("You may drink because you are " + x + " years old.");
isAgeReached = true;
}
}

Or still simpler :

int x = 0;
while (x < 21) {
System.out.println("You cannot drink because you are only " + x + " years old.");
x++;
}

System.out.println("You may drink because you are " + x + " years old.");

GoTo Next Iteration in For Loop in java

continue;

continue; key word would start the next iteration upon invocation

For Example

for(int i= 0 ; i < 5; i++){
if(i==2){
continue;
}
System.out.print(i);
}

This will print

0134

See

  • Document

What will this continue cause the nested for-loop to do?

If there was a code under continue, it'll be a dead code (unreachable code).

The way you wrote it, it has no effect since it's already the last line. The loop would have continued if there was no continue;.

These two blocks of code have the same effect:

for(int i = 0; i < 10; i++) {
// .. code ..
}

for(int i = 0; i < 10; i++) {
// .. code ..
continue;
}

However, the below snippet has unreachable code:

for(int i = 0; i < 10; i++) {
// .. code ..
continue;
// .. unreachable code ..
}


Related Topics



Leave a reply



Submit