In this tutorial, we will learn about the Jump Statements in Java, or its types, and how to use them with the help of examples.
Define Jump Statements
Jump statements are use to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily use to interrupt loop or switch-case instantly.
Java supports three jump statements:
- Break Statement
- Continue Statement
- Return Statement.
Break Statement in Java
The Break Statement is use to exit the loop irrespective of whether the condition is true or false. Whenever a “break” is encountered inside the loop, the control is sent outside the loop.
Syntax
Syntax of Break Statement in Javabreak;
Example of Break Statement in Java
Input Value
Example of Break Statement in Javapublic class jumprbc { public static void main(String args[]) { int i; for(i = 1; i <= 15; i++) { if (i == 10) break; System.out.print(i + " "); } } }
Let’s run the code
Continue Statement in Java
The continue statement is use to immediately move to the next iteration of the loop. the control is taken to the next iteration thus skipping everything below “continue” inside the loop for that iteration.
Syntax
Syntax of Continue Statementcontinue;
Example of Continue Statement
Input Value
Example of Continue Statementpublic class jumprbc { public static void main(String[] args) { for(int i=5;i<=15;i++){ if(i==10){ continue; } System.out.println(i); } } }
Let’s run the code
Output Value
Return Statement in Java
The Return Statement of the return value must match the method declare return statement. we can’t return an integer value from a method whose declaration types is void.
Syntax of Return Statement in Java
Syntax of Return Statement in Javareturn;
Example of Return Statement in Java
Input Value
Example of Return Statement in Javapublic class jumprbc { public static int add(int x, int y){ int sum = x+y; return sum; } public static void main(String[] args) { int a = 25; int b = 54; int sum = add(a, b); System.out.println("Sum x and y: " + sum); } }
Let’s run the code
Output Value
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about JAVA Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments