Jump statement in Java :-
This tutorial is explain about what is Java Jump statements and that's type and how to use with programming example in Java.
Jump Statements in Java: Java support three jump statement: break, continue and return. These three statement transfer control to another part of the program.
Break Statement:- Break statement has three use in Java.:-
1. its terminate a statement sequence in switch case.
2. it can be used to exit a loop.
3. it can be used to civilized from of goto.
Break to Exit a loop:- you can immediate to terminate from the loop by using of the break keyword. When break statement encountered inside the loop then loop is terminated and control resume at the next statement.
class BreakExample{
public static void main(String [] args){
for(int i=0; i<10; i++){
if (i== 5) break;
System.out.println(“\n Value:- ”+i);
}
}
}
Output:-
Value:- 0
Value:- 1
Value:- 2
Value:- 3
Value:- 4
Break as a form of goto:- break statement is also used as “civilized” form of the goto statement. Java does not have a goto statement. so we use there as break and after use a label for to move on next statement.
Example:- break label;
class BreakExample{
public static void main(String [] args){
outer: for (int i=0; i<3;i++){
System.out.println(“ Outer Value:- ”+i);
for(int j=0; j<10; j++){
if (i== 5) break outer;
System.out.println(“ ”+j);
}
}
}
}
Output:- Outer Value:- 0 1 2 3 4
2. 2. Continue Statement: - It is useful to force an early iteration of a loop. You might want to continue running the loop but stop processing the remainder of the code in its body for this particular iteration. In while loop and do while loop continue keyword to conditional expression that control a loop. Intermediate code for all these three loop bypass.
class ContinueExample{
public static void main(String [] args){
for(int i=0; i<10; i++){
System.out.println(i+“ ”);
if (i%2==0) continue;
System.out.println(“ ”);
}
}
}
Output:-
0 1
2 3
4 5
6 7
8 9
3. 3. Return Statement:- It is used to explicitly return from a method. It is control the program and transfer back to the caller of the method. When use return in method then method is perform its task and back to the caller methods. return causes execution to return to the Java run time system that is called main method.
class Addition{
int Sum(int a, int b){
int c;
c=a+b;
return c;
}
public static void main(String [] args){
int x=2;
int y=3;
int z= Sum(x, y);
System.out.println("sum= "+z);
}
}
Output:- 5
1 Comments
This comment has been removed by the author.
ReplyDeleteHello Friends, Please comment and share and do not write spam messages