Jump statements in Java
Jump statements in Java
Jump statements are used to skip some piece of code or we can say jump statement is used to jump from one coding location to another coding location.
Usually it is used to interrupt loop or switch statement and also can interrupt a method. Few jump statements are mentioned below.
Usually it is used to interrupt loop or switch statement and also can interrupt a method. Few jump statements are mentioned below.
- continue
- break
- goto
- return
continue this is used to continue that on going process which is met to continue statement.
- In for loop we can place continue to proceed further operations.
- In while and do while loops we can also place continue statement to skip some iteration.
Syntax: continue;
Program:
public class Test {
public static void main(String[] args) {
for(int i=0 ; i<10; i++){
if(i==5) {
continue;
}
System.out.println("while loop iteration no:"+i);
}
}
}
Output:
while loop iteration no:0
while loop iteration no:1
while loop iteration no:2
while loop iteration no:3
while loop iteration no:4
while loop iteration no:6
while loop iteration no:7
while loop iteration no:8
while loop iteration no:9
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
break this is used to break the iteration of the loop and the switch case also.
Syntax: break;
Program:
public class Test {
public static void main(String[] args) {
for(int i=0 ; i<10; i++){
if(i==5) {
break;
}
System.out.println("while loop iteration no:"+i);
}
}
}
Output:
while loop iteration no:0
while loop iteration no:1
while loop iteration no:2
while loop iteration no:3
while loop iteration no:4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
goto this is used to jump from one location to another, as it is clear with it's name.
Note: goto is a reserve keyword in java and not allowed in most programming languages and java also does not allow because it is become very difficult to trace the control flow and program flow, program readability becomes very difficult.
Syntax: goto label;
//some code;
label: statement;
We can use label which is work something like that-
Example:
/**
* @author Suraj Pratap Singh
*/
public class Test {
public static void main(String[] args) {
for(int i=0 ;i<5; i++) {
System.out.println("value of i: "+i);
lbl:
for(int j=0; j<5; j++) {
if(j==3) {
break lbl;
}
System.out.println("\t\t\tvalue of j: "+j);
}
}
}
}
We can use label which is work something like that-
Example:
/**
* @author Suraj Pratap Singh
*/
public class Test {
public static void main(String[] args) {
for(int i=0 ;i<5; i++) {
System.out.println("value of i: "+i);
lbl:
for(int j=0; j<5; j++) {
if(j==3) {
break lbl;
}
System.out.println("\t\t\tvalue of j: "+j);
}
}
}
}
Output:
value of i: 0
value of j: 0
value of j: 1
value of j: 2
value of i: 1
value of j: 0
value of j: 1
value of j: 2
value of i: 2
value of j: 0
value of j: 1
value of j: 2
value of i: 3
value of j: 0
value of j: 1
value of j: 2
value of i: 4
value of j: 0
value of j: 1
value of j: 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return this is a reserve keyword and used to return some value from the method, and it is also used to terminate the method also.
Syntax: return;
Program 1:
/**
* @author Suraj Pratap Singh
* @purpose This is an example of return some value from method.
*/
public class Test {
public static String getMyName(String str) {
return str;
}
public static void main(String[] args) {
String str="Suraj Pratap Singh";
System.out.println(getMyName(str));
}
}
Program 2:
/**
*
* @author Suraj Pratap Singh
* @purpose This is an example to terminate method.
*/
public class Test {
public static void demoMethod(int num) {
for(int i=0; i<num; i++) {
if(i==5) {
return;
}
System.out.println(i+" times executed.");
}
}
public static void main(String[] args) {
demoMethod(10);
}
}
/**
*
* @author Suraj Pratap Singh
* @purpose This is an example to terminate method.
*/
public class Test {
public static void demoMethod(int num) {
for(int i=0; i<num; i++) {
if(i==5) {
return;
}
System.out.println(i+" times executed.");
}
}
public static void main(String[] args) {
demoMethod(10);
}
}
Output:
0 times executed.
1 times executed.
2 times executed.
3 times executed.
4 times executed.
Comments
Post a Comment