20/01/2023

What are the 3 Types of Jump Statements in Java?

For transferring the control of the program from one point to any other part of the program unconditionally, jump statements are used. The main purpose of the jump statement is to interrupt the switch case or loop instantly. Three types of jump statements are supported in java.

3 Types of Jump Statements in Java

  • The break Statement
  • The continue Statement
  • The return Statement

1. The Break Statement


The break statement in Java is used for breaking out the loops or switch-case from the middle. A break statement can be used for breaking the flow of for, do, while loop or switch case. Current loop execution is stopped immediately after the encounter of the break statement.

And the program control goes to the first statement that is following the loop in the program. A break statement allows us to force the immediate loop termination bypassing the execution of the remaining iteration and remaining code of the loop.

The main application of the break statement is the early exiting of the switch case or loop by skipping the remaining statement execution.

Syntax of break statement is:

break;

  • More than one break is allowed in a single loop
  • Only the current loop is terminated by the break statement

Example:

// program to demonstrate the example of a break statement in printing numbers from 1 to 10

class BreakStatementExample {

    public static void main(String args[]) {

        System.out.println("Welcome to the break statement example");

        for (int i = 1; i <= 10; i++) {

            System.out.println("value of i = " + i);

            if (i == 5) {

                System.out.println("Loop terminated by break statement..bye!!");

                break;

            }

        }

        System.out.println("Outside the loop");

    }

}


Output:

Welcome to the break statement example

value of i = 1

value of i = 2

value of i = 3

value of i = 4

value of i = 5

Loop terminated by break statement..bye!!


Outside the loop


Explanation: In the above program, we are executing the loop from 1 to 10 and inside the loop, we are printing the value of i. And here in the loop one condition is given(i==5) and inside this, some statement is printed and a break statement is used.

So the loop terminates at the 5 value of i. And no further iterations and statements of the loop will be executed. And the statement written outside the loop will be executed.

Using Break as a Form of Goto


If the break statement is used in the inner loop of the nested loop then the break statement will terminate the inner loop and send the program control to the outside loop only. But in some cases, it is required to transfer the control of the program outside of both the inner and outer loop then for that, another form of break statement is provided by java which is known as the labelled break statement.

Labelled break statement in java allows us to specify the location where we want to transfer the control of the program. Labelled break statements enable the use of break statements as a form of a goto statement.


As a goto statement is not there in the Java programming language. Labelled break statement allows the transfer of control outside to the deeply nested loop and it can also be used in breaking one or more than one code block.

Syntax of Labeled break statement is:

break label;

Here the label specifies the label name by which the code block is identified to which control is needed to be transferred.

To name a code block i,e, to be executed after transferring the control from the break statement simply put the name of the label at the start of it. Any valid identifier of java is allowed to be used as a label and it is followed by a colon. After labelling the block, this label can be used as the target label of the break statement.

2. The Continue Statement


Similar to the break statement, the remaining statement of the loop is also skipped by the continue statement but the continue statement does not terminate the execution of the loop, instead, it transfers the control of the program to the loop beginning for the execution of the next iteration of the loop. And the further loop execution is continued until the loop testing condition becomes false.

In the case of continue, the statement is used with a while and do-while loop then after the continue statement immediately there is an evaluation of the testing condition of the loop but in the case of using the continue statement in for loop. Immediately after the continue statement increment/decrement operation is evaluated then there is an evaluation of the testing condition.

Syntax of continue statement is:

continue;

Example:


// program to demonstrate the example of a continue statement in printing numbers from 1 to 10

class ContinueStatementExample {

    public static void main(String args[]) {

        System.out.println("Welcome to the continue statement example");

        for (int i = 1; i <= 10; i++) {

            if (i == 5) {

                System.out.println("skipping iteration by continue statement!!");

                continue;

            }

            System.out.println("value of i = " + i);


        }

    }

}

 

Output:


Welcome to the continue statement example

value of i = 1

value of i = 2

value of i = 3

value of i = 4

skipping iteration by continue statement!!

value of i = 6

value of i = 7

value of i = 8

value of i = 9

value of i = 10

 

Explanation: Above is the program for printing numbers from 1 to 10. We are running a loop from 1 to 10 and using the continue statement in the condition i==5 so when the value of i becomes 5 statements of the loop following the continue statement is skipped. I,e, here System.out.println(“value of i = “+i); statement is skipped and loop execution is started again from the next iteration i.e. from the 6 value of i.

3. The Return Statement


The return statement in java is used for method termination and for transferring the control of the program back to the calling method. Return is an optional statement if the return type of the method is void.

But if the method does not void then the return statement is used to return the value of the specified return type from the method. If the return type is void and the return statement is not written, then control is transferred back to the calling method after the complete execution of the method. Values may or may not be returned by the return statement to the calling method.

Example:


// program to demonstrate the example of a return statement in printing numbers from 1 to 10

class ReturnStatementExample{

public static void printValues(){

for(int i =1; i<=10; i++){

System.out.println("value of i = "+i);

             if(i==5){

return;

            }

}

System.out.println("outside the loop");

}

public static void main(String args[]){

     System.out.println("Welcome to the calling function");

         printValues();

        System.out.println("Control is returned back from the called function");

  }

}


Output:

Welcome to the calling function

value of i = 1

value of i = 2

value of i = 3

value of i = 4

value of i = 5

Control is returned back from the called function

 

Explanation: Above is the program for printing numbers from 1 to 10 by calling a function. In the called function we are running a loop from 1 to 10 and using the return statement in the condition i==5 so when the value of i becomes 5 execution of the loop is terminated and control of the program is returned to the calling function without execution of any further statement of the called function.
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: