×
☰ See All Chapters

Jump Statements in Java

Jump statements are used to jump the control to another part of our program depending on the conditions. Jump statements are useful to break the iterations loops when certain conditions met and jump to outside the loop. These statements can be used to jump directly to other statements, skip a specific statement and so on.

Following are the jump statements in java.

  • break 

  • continue 

  • return 

  • goto (reserved for future use, currently not in use) 

break statement

break statement syntax

break;

If break statement is encountered then execution of its immediate surrounding block will be skipped control will go to next statement immediately after that block.

break statement example

package com.java4coding;

 

public class HelloWorld {

 

        public static void main(String[] args) {

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

                        if (i == 3 ) {

                                break;

                        }

                        System.out.println("The number is " + i);

                }

        }

}

Output:

The number is 1

The number is 2

labeled break statement

We can also use the break statement with a label. The break statement is used when we want to transfer the flow of control from an inner block to an outer block, but when we want the flow of control to exit the outer block then we use a labeled break statement.

labeled break statement example

package com.java4coding;

 

public class HelloWorld {

        public static void main (String args[])  

          {  

             boolean a=true;  

             a:  

             {  

                b:  

                {  

                   c:  

                   {  

                      System.out.println("Before break");  

                      if(a)  

                      break a;  

                      System.out.println("This will not execute");  

                   }  

                }  

                System.out.println("After break");  

             }  

          }  

 

}

Output:

Before break

After break

continue statement

continue statement syntax

continue;

If continue statement is encountered then execution of remaining statements in its immediate surrounding block will be skipped. Unlike break, it skips the remaining statements not the complete block, so if the block is iterative block then control goes to the beginning of the loop.

continue statement example

package com.java4coding;

 

public class HelloWorld {

 

        public static void main(String[] args) {

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

                        if (i == 3 ) {

                                continue;

                        }

                        System.out.println("The number is " + i);

                }

        }

 

}

Output:

The number is 1

The number is 2

The number is 4

The number is 5

return

This is used to return value from a method. Once method returns a value, further statements after return statements will be skipped.

More about this will be studied later while discussing about methods.

package com.java4coding;

 

public class HelloWorld {

 

        public static void main(String[] args) {

                System.out.println(printName());

        }

 

        public static String printName() {

                String names[] = { "Manu", "Advith", "Likitha" };

 

                if (names.length == 3) {

                        return "Advith";

                }

 

                for (String name : names) {

                        System.out.println(name);

                }

 

                return "Manu";

        }

 

}

 

Output:

Advith

 


All Chapters
Author