05
OctJump Statements in JAVA - Types of Statements in JAVA (With Examples)
Jump Statements in JAVA: An Overview
Jump statements in Java are control flow statements that let you change the order in which programs execute. These statements provide flexibility and control over program logic to the programmer. If you are looking to enhance your Java programming skills and gain a comprehensive understanding of jump statements, consider enrolling in Full Stack Java Developer Course. In this Java tutorial, we'll cover these statements and how they're used with Java.
What are Jump Statements in Java?
In Java, a jump statement is used for transferring the control of the program from one particular point to another point of the program, allowing for changes in the flow of execution.
This particular statement works by jumping from one specific code to another one, thus exhibiting polymorphism in Java, to the flow of the execution of the program. "Jump Statements" are also called "Branching Statements in Java" as they evaluate different branches to enhance the flow of the execution.
Read More - Advanced Java Interview Questions
Types of Jump Statements in Java
There are three major variations of the jumping statements in JAVA. Those are "Break statement", "Continue statement" and "Return statement". These will be discussed further in this article. Not only that we’ll give you a clear idea about Java constructor in our next article.
Read More: Looping Statements in Java
1. Break statement in Java
- A break statement in Java assists in terminating the execution of a loop in a particular program.
- This statement transfers the control of the program to the next statement which is immediately after the current loop or switch.
- A break statement is used in an existing statement of a loop, especially an infinite loop, to exit from it.
- There are 3 types of Break Statements, those are "Exiting a Loop", "As a form of Goto", and "In a switch case".
Syntax
break;
Read More - Java Developer Salary
Example: Use Break to Exit a Loop
class break_statement
{
public static void main(String[] args)
{
int i=1;
for (;;i++) //infinite loop
{
if (i==5)
{
break;
}
else
{
System.out.println(i);
}
}
}
}
Output
1
2
3
4
Example: Use Break as a form of goto
class break_statement_goto
{
public static void main(String[] args)
{
label_1:
{
label_2:
{
label_3:
{
for(int i=0; i<100; i++)
{
System.out.println("Inside Label_3");
if (i==3)
break label_3;
}
}
System.out.println("Inside Label_2");
}
System.out.println("Inside Label_1");
}
}
}
Output
Inside Label_3
Example: Use Break in a Switch case
class Switch_Case
{
public static void main(String[] args)
{
char a = 'C';
switch (a)
{
case 'A':
System.out.println("Letter A");
break; //break statement to come out of switch
case 'C':
System.out.println("Letter C");
break;
default:
System.out.println("Default case: NO Letter Matched!");
}
}
}
Output
Letter C
Read More: Switch Statement in Java
2. Continue statement in Java
- The continue statement in Java never terminates the execution of any loops.
- It can only work inside the "loop statement".
- The primary job of the "Continue statement" is the iteration of that specific loop.
- Continue statement assists the bypass of all the other statements by making them fall under it.
- The nature of the Continue statement is to skip the current iteration and force for the next one.
Syntax
Continue;
Example
class continue_statement
{
public static void main(String[] args)
{
for(int i=0; i<=10;i++)
{
if (i<3)
{
Continue; //continues and goes to next iteration
}
System.out.println(i);
}
}
}
Output
3
4
5
7
8
9
10
3. Return statement in Java
- The return statement in Java is a type of jump statement which is primarily used only inside the functions or methods.
- If in a method, any code is written after using the Return statement that can be treated as an unreachable statement by the compiler.
- The purpose of using a Return statement is to terminate the current method of execution and transfer the control to the next "calling method".
- There are two types of Return statements, which are "Return with a value" and "Return without a value".
Syntax
return;
Example
class continue_statement
{
public static void main(String[] args)
{
int age = 12;
System.out.println("Using Return");
if (age<18)
return; //terminates the method
System.out.println("Will not get executed!");
}
}
Output
Using Return
Summary
It's important to understand jump statements in Java, as they are essential tools in a programmer's toolkit. With the break, continue, and return statements at your disposal, you can ensure that your code is efficient, optimized, and easily readable. As each of these statements has its unique applications, it's important to understand where and when to make use of them, which is a valuable skill to acquire during Full Stack Java Course.