• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Structures » Jump Statements in Java Example
Next →
← Prev

Jump Statements in Java Example

By Dinesh Thakur

In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop or switch-case instantly. Java supports three jump statements: break, continue, and return.

We’ll be covering the following topics in this tutorial:

  • THE break Statement
  • THE continue STATEMENT
  • THE Return Statement

THE break Statement

The break construct is used to break out of the middle of loops: for, do, or while loop. When a break statement is encountered, execution of the current loops immediately stops and resumes at the first statement following the current loop. That is, we can force immediate termination of a loop, bypassing any remaining code in the body of the loop.It is mostly used to exit early from the loop by skipping the remaining statements of loop or switch control structures. It is simply written as
break;

• We can have more than one break statement in a loop.
• The break command terminates only the current loop and not any enclosing loops.

class BreakStatement {
public static void main(String args[]){
        System.out.println(“Show importance of break statement”);
            for(int i =1; i<=10; i++){
System.out.println(“i = “+i);
                if(i==5){
                    System.out.println(“\nBye”);
break;
               }
           }
     }
}

Output: Show importance of break statement 1 2 3 4 5
i = 1
i = 2
i = 3
i = 4
i = 5
Output:
Bye
Explaination: In this program, the for loop executed starting from i = 1 to 10 in steps of 1. Now when the condition (i==5) in the body of the loop is satisfied, the break statement causes the control to move out of for loop.

Program to input indefinite numbers and then calculate the sum of only the positive numbers. The program terminates when negative number is input?

//program to show sum of indefinite numbers
import java.util.scanner;//program user scanner class
public class SumIndefinite {
      public static void main(String[] args){
int num, sum =0;
//Create Scanner object to obtain input from keyboard
           Scanner input =newScanner(system.in);
system.out.print(“Enter numbers(negative number to quit) —>”);
while(true){
num = input.nextInt();//Read number
              if(num <0)
                 break;
sum += num;
          }
system.out.println(“Sum is —–>”+sum);
      }
}

Output: Enter numbers(negative number to quit) —> 50 21 33 17 -1
Sum is —>121

Explanation: This program computes the sum of positive numbers input by the user. When a negative number is an input, the condition (num < 0) become true and break statement executed which leads to the termination of the while loop and the next statement following the loop executed which displays the sum of positive numbers. The condition of the while loop always remains true as we have specified a non-zero value 1 which makes it run infinitely. The only way to exit this loop is to use a break statement.

In the nested loops, if the break statement occurs in the inner loop then the control is transferred only out of the inner loop, and it has nothing to do with the rest of the surrounding looping statements. However, in some cases, we need to jump not only out of the inner loop but also from the outer loop(s). In such a case, Java provides another form of break statement known as a labeled break statement. It allows you to specify from which loop you want to break. The labeled break statement enables you to jump immediately to the statement following the end of any enclosing statement block or loop that is identified by the label in the labeled break statement regardless of how many levels of nested blocks are there.

Before you use a labeled break statement, one should label the statement block or loop you want to exit from. To label a block or loop, you put a label (i.e.label name) followed by a colon at the start of it. Once you have labeled a block or loop, you can use this label along with the break statement. The general form of the labeled break statement is
break label;
On execution, it causes to exit out of the labeled block or loop and resume with the next statement after the labeled break or loop.

Using break as a form of Goto

The break statement can also be used to act as another form of the goto statement. Java does not have a goto statement, as it leads to unstructured programming which is less readable. To come out of a deeply nested set of loops, we can use the labeled break statement. We can also use it to break out of one or more blocks of code. We can also specify precisely the location from where execution should resume because this form of break works with a label as shown :
break label;
the label is the name of a label that identifies a block of code. When this form of break executes, control transferred out of the named block of code. The labeled block of code must enclose the break statement, but it does not need to be the immediately enclosing block. However, we cannot use a break to transfer control to a block of code that does not enclose the break statement.
To name a block, put a label at the start of it. A label is any valid Java identifier followed by a colon. Once we have labeled a block, we can then use this label as the target of a break statement. Doing so causes execution to resume at the end of the labeled block.

THE continue STATEMENT

Like the break statement, the continue statement also skips the remaining statements of the body of the loop where it is defined but instead of terminating the loop, the control is transferred to the beginning of the loop for next iteration. The loop continues until the test condition of the loop becomes false.
When used in the while and do-while loops, the continue statement causes the test condition to be evaluated immediately after it. But in case of for loop, the increment/decrement expression evaluates immediately after the continue statement and then the test condition is evaluated.
It is simply written as
continue;

/* Print Number from 1 to 10 Except 5 */
class NumberExcept {
      public static void main(String args[] ) {
int i;
            for(i=1;i<=10;i++) {
if(i==5) continue;
                     System.out.print(i +” “);
            }
}
}

Above program will display the value of variable i from 1 to 4. When the value of variable i becomes 5, continue statement will skip the body of the loop following continue statement i.e. it skips System.out.println(i) statement and again executes the loop with the next iteration (value) i.e .. 6.

THE Return Statement

This statement is mainly used in methods in order to terminate a method in between and return back to the caller method. It is an optional statement. That is, even if a method doesn’t include a return statement, control returns back to the caller method after execution of the method. Return statement mayor may not return parameters to the caller method.

You’ll also like:

  1. Jump Statements in c++ or Goto statement
  2. Control statements in java
  3. Nested Switch Statements Java Example
  4. Conditional Statements in C++ or if Statements
  5. Nested Try Statements in Java Examples
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW