Introduction
In Java jump statements are mainly used to transfer control to another part of our program depending on the conditions. These statements are very useful from the programmer's view because these statements allow alteration of the flow of execution of the program. These statements can be used to jump directly to other statements, skip a specific statement and so on. In Java we have the following three jump statements:
- break (simple and labeled)
- continue
- return
The break statement
If we want to go out of a loop then we use a break statement. If we use a break statement in a loop then execution will continue with the immediate next statement outside the loop. After a break, all the remaining statements in the loop are skipped. The break statement can be used in a while loop, for loop, do-while loop, and in a switch case.
Syntax
- if(condition)
- {
- break;
- }
- package myclass1;
- class Myclass1
- {
- public static void main(String args[])
- {
- int n = 1;
- while (n <= 15)
- {
- System.out.println("\n" + n);
- n++;
- if (n == 7)
- {
- break;
- }
- }
- }
- }
The 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 loop to an outer loop, but when we want the flow of control to exit the outer loop then we use a labeled break statement. A labeled break statement is somehow similar to the goto statement.
Syntax
- if(condition)
- break label_name;
- package myclass1;
- class Myclass1
- {
- public static void main (String args[])
- {
- boolean a=true;
- m:
- {
- n:
- {
- o:
- {
- System.out.println("Before break");
- if(a)
- break n;
- System.out.println("This will not execute");
- }
- }
- System.out.println("After break");
- }
- }
- }

Join the conversation! Your thoughts help the community grow.