Latest :

Java Continue Statement – Tutorial & Examples

Java continue complete tutorial with examples. If you have any doubts related to Java continue do leave a comment here.

Definition:

‘continue’ is a statement used to skip the remaining statements in the current iteration and move to next iteration of a loop.

The looping construct can be a while loop, for loop or a do-while loop. Without a loop, we cannot use ‘continue’.

‘break’ can be used both in switch and loop directly but continue cannot be used in switch directly. If a switch is inside a loop then continue can be used in the switch. In that case the continue will be applied on the loop only.

Java Continue In Any Loop

Java Continue can be used to move to next round (iteration) of a loop from middle of the body by skipping the statements of current round (iteration).

Generally next round is started when all statements of the loop body are completed. But sometimes we may need to skip running iteration when a particular situation arise in middle of the body. So, continue is always used conditionally only.

Java Continue Example: 1

when condition2 is true, continue is executed and Statement2 is skipped.

So, from continue, the control directly moves to condition1. In this kind of setup, Statement1 is executed in each iteration and Statement2 is executed only when continue is not executed.

Java Continue Example: 2

In the above example, the loop is repeatedly executed as long as (a<=b) results in true. In each iteration, a++ is executed without fail.

But printing the a value does not take place each time. Printing takes place only when (b%a==0) results in false. If the condition (b%a==0) is true, continue is executed and printing is skipped.

Example :3 

In the above example, the do-while is executed as long as (x<=y) in case (a==c) is true then a is assigned with d and control jumps to the condition (x<=y) it means b=x++; is skipped. Irrespective of truth value of (a==c), the statement a=x++; is executed.

Behavior Of Continue In While And For

In while and do-while loops, the continue statement takes the control to condition part (by skipping remaining body) but in for loop, the continue statement takes the control to incrementation part.

So, If the loop control variable is modified after the continue statement, the loop may become infinite in while and do-while. But in for loop that problem will not arise as modifying the loop control variable takes place in incrementation part.

Example:

The above loop will be an infinite loop. The following loop, with the same mechanism, will be executed normally as incrementation takes place whether continue is executed or not.

Example: 4

Continue In An Inner Loop

Continue is always applied on the current loop. When a loop is inside another loop and continue is executed in the inner loop, then the control jumps to next iteration of inner loop only.

If we want to jump to condition part of outer loop then we have to write another continue statement, with appropriate condition, in the outer loop.

Example:

In the above example, when (a[i][j]!=b[i][j]) is true the corresponding continue will go to next iteration of inner loop (but not the outer loop) by skipping StatementX.

Outside the inner loop, we have written another continue, conditionally, to jump to next iteration of outer loop. If the condition (j<cols) is true then the outer loop continues by skipping the StatementN.

Continue With A Label In Loops

If we want to apply it on the outer loop directly from that condition then we can use a continue statement with a label where the label represents the outer loop. The name of the label is our choice just like a variable.

Eg:          ROWS:

In this example, the “continue ROWS” will be applied on the outer (ROWS) loop. This facility will help us to minimize the code and maximize readability of the program.

Continue Should Not Be Used In Absence Of A Loop

A continue must be used inside a loop only. Without a looping construct, we should not use continue. Suppose we use continue in an if construct where the if is not in a loop then a compiler error will be raised.

Java Continue Statement Example:

This program will raise an error because there is no loop but continue is used. Similarly the following program also will raise a compilation error.

Example: 7

x

Check Also

Constructors In Java – Types & Examples | JavaTutorials

A constructor is a special method that is invoked when a new object is created. ...