Java Jump Statements - The Coding Shala

Home >> Learn Java >> Java Jump Statements

Jump Statements

In Java, jump statements are used to transfer control to another part of the program. There are three types of jump statements available in Java. They are break, continue, and return.
Java Jump Statements - The Coding Shala

Java break Statement

Java break statements are basically used to exit a loop or can be used as goto statements with labels. Java break statements also used to terminate a statement sequence in a switch statement.

The following Java program used the break statement to exit the loop: 

class BreakExample{
    public static void main(String args[]){
        for(int i=0;i<=100;i++){
            if(i==10){ //exit loop when i is equals to 10
                break;
            }
            System.out.println("Value of i is: " + i);
        }
    }
}



<<<<<Output of this program is >>>>>
Value of i is: 0
Value of i is: 1
Value of i is: 2
Value of i is: 3
Value of i is: 4
Value of i is: 5
Value of i is: 6
Value of i is: 7
Value of i is: 8
Value of i is: 9

Note: If a break statement is used inside nested loops, the break statement will only break out of the innermost loop.

We can also use break statements as goto that means using break we can jump one or more blocks of code. The basic syntax is as follows: 

break label;

Here, the label is the name of a label that identifies a block of code. The labeled block must enclose the break statement. 

The following Java program explains it: 
class BreakExample{
    public static void main(String args[]){
        boolean tc = true;
        first : {
            second : {
                third : {
                    System.out.println("Before the break statement");
                    if(tc){
                        System.out.println("Break statement to jump the first label");
                        break first;
                    }
                }
                System.out.println("This part is skipped");
            }
        System.out.println("Skipped");
        }
        //control here
    }
}



<<<<<Output of this program is>>>>>
Before the break statement
Break statement to jump the first label

Java Continue Statement

Java continues statement is used to skip the code execution after a continue statement for a particular iteration. It does not stop the loop running. It just skips an iteration. The following program explains it: 

class ContinueExample{
    public static void main(String [] args){
        for(int i=0;i<10;i++){
            System.out.print(i + " ");
            if(i%2==0){  //skip rest of the code if i is even
            continue;
            }
            System.out.println("");
        }
    }
}




<<<<output of this program is>>>>>>
0 1 
2 3 
4 5 
6 7 
8 9 

Java return Statement

Java return statement is used to return from a method that means used to transfer the control back to the caller of the method. The return statement immediately terminates the method in which it is executed. The following Java Program will explain it: 

class ReturnExample{
    public boolean checkEven(int num){
        if(num%2 == 0) {
            System.out.println("Even number");
            return true; //exit form method
        }
        else{
            num = num+1;
            System.out.println("num after adding 1 is: "+ num);
        }
        return false;
    }
    
    public static void main(String [] args){
        int num1 = 8;
        int num2 = 9;
        ReturnExample rt = new ReturnExample();  //in later chapters
        rt.checkEven(num1);
        rt.checkEven(num2);
    }
}



<<<<<Output of this program is>>>>>>
Even number
num after adding 1 is: 10



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala