Java – Jump Statements

Java – Jump Statements 2017-08-09T10:01:57+00:00

Java jump statements are break, continue, and return. These statements transfer control to another part of your program.

1. break.
2. continue.
3. return.

1 The break statement

  • This statement is used to jump out of a loop.
  • Break statement was previously used in switch – case statements.
  • On encountering a break statement within a loop, the execution continues with the  next statement outside the loop.
  • The remaining statements which are after the break and within the loop are skipped.
  • Break statement can also be used with the label of a statement.
  • A statement can be labeled as follows.

statementName : SomeJavaStatement

  • When we use break statement along with label as

break statementName;

The execution continues with the statement having the label. This is equivalent to a goto statement of c and c++..

An example of break statement


Output :
An example of break to a label


Output :
Before the break
This is after b

2 Continue statement

  • This statement is used only within looping statements.
  • When the continue statement is encountered, the next iteration starts.
  •  The remaining statements in the loop are skipped. The execution starts from the top of loop again.

The program below shows the use of continue statement.


Output :

3 The return statement

  • The last control statement is return. The return statement is used to explicitly return from a method.
  • That is, it causes program control to transfer back to the caller of the method.
  • the return statement immediately terminates the method in which it is executed.

The program below shows the use of return statement.


Output :
Before the return.
NOTE : the if(t) statement is necessary. Without it, the Java compiler would flag an “unreachable code” error, because the compiler would know that the last println( ) statement would never be executed. To prevent this error, the if statement is used here to trick the compiler for the sake of this demonstration.

Prev Next