Intro: Hi Dear, In this post learn in details what is Jump Statements:-
Extra Important Statements
Jump Statements:
- It is used to transfer the control from one point to another point in the program.
There are three Jump Statement are used in java.
- break Statement
- continue Statement
- return Statement
Break Statement
- It is used to transfer the control out of the body of loop.
- In another word we can say that it is terminates.
- Break Statements are mostly used with loop(for, while, do-while) and switch statements.
Example of Break Statements:-
Example 1: Print without using break statements.
Print 1 to 10 numbers
class Jump{
public static void main(String arg[]){
for(int i=1; i<=10; i++){
System.out.print(" "+i);
}
}
}
Example 1.2: Program with break Statements .
class Break{
public static void main(String arg[]){
for(int i=1; i<=10; i++){
System.out.print(" "+i);
if(i==5){ // if i=5 then break
break;
}
}
}
}
Continue Statements
- It is used to skip the next statements and continue the loop.
- Continue statements are mostly used with loop(for, while, do-while).
An Example of Continue Statements:
Example 2.1: Program with continue statements
class Continue{
public static void main(String arg[]){
for(int i=1; i<=10; i++){
if(i==5) // if i=5 then skip only 5 so next print
continue; // using continue statements
else
System.out.print(" "+i);
}
}
}
Output: 1 2 3 4 6 7 8 9 10
Example 2.2:
// Example 5.4: Program Continue but only print one Statement
class Continue1{
public static void main(String arg[]){
for(int i=1; i<=10; i++){
if(i!=5) // Only print 5 and all skip.
continue;
else
System.out.print(" "+i);
}
}
}
Output : 5
Example 2.3
class Continue2{
public static void main(String arg[]){
for(int i=1; i<=10; i++){
if(i<=5) // if i<=5 print 6,7,8...
continue;
else
System.out.print(" "+i);
}
}
}
Output: 6,7,8,9,10
Example 2.4
class Continue3{
public static void main(String arg[]){
for(int i=1; i<=10; i++){
if(i==5) // skip 5 then next
continue;
else
System.out.print(" "+i);
}
}
}
Output: 1 2 3 4 6 7 8 9 10
Return Statements
- return statements terminates the current function and transfer the control to the calling function.
- we can also use return statements to transfer value one function to another.
An Example of Return statements:
Example 1:
class Return {
int sum(){
int a=10, b=20;
return a+b;
}
public static void main(String args[]){
Return sc= new Return();
int rs= sc.sum();
System.out.println("Addition =" + rs);
}
}
Output:- Addition = 30
All Right guys i hope this is helpful for you, so don't forget follow me.
Comments