You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Jumps statements are used to jump to a specific line in a program. There are two type of jumps:
9
+
Jumps statements are used to jump to a specific line in a program.
10
10
11
-
- Unconditional jumps
12
-
- Conditional jumps
11
+
Java supports three jump statements.
12
+
-`break`
13
+
-`continue`
14
+
-`return`
15
+
16
+
These three statements transfer control to other part of the program.
17
+
18
+
Let's see one by one how it works.
13
19
14
-
### **Unconditional Jumps**
20
+
## **Break statements**
21
+
Break Statement is a loop control statement that is used to terminate the loop. As soon as the `break` statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop.
15
22
16
-
Unconditional jumps are used to jump to a specific line in a program.
17
-
It has a single parameter, the line number to jump to. there are two types of unconditional jumps:
23
+
Example.
24
+
```java
25
+
publicclassHelloWorld {
26
+
publicstaticvoidmain(String[] args) {
27
+
// Initially loop is set to run from 1-5
28
+
for(int i=1; i<=5; i++){
29
+
// terminate loop when i is 4.
30
+
if(i=4){
31
+
break;
32
+
System.out.print(i);
33
+
}
34
+
}
35
+
}
36
+
}
18
37
19
-
-`goto`
20
-
-`continue`
38
+
```
39
+
Output: `1 2 3`
21
40
22
-
### **Conditional Jumps**
41
+
In Java, `break` is majorly used for:
42
+
- To exit a loop.
43
+
- Terminate a sequence in a `switch` statement.
23
44
24
-
Conditional jumps are used to jump to a specific line in a program.
25
-
It has two parameters, the line number to jump to and a condition. there are two types of conditional jumps:
45
+
## **Continue Statements**
46
+
Sometimes you doesn't want to execute a particular iteration in a loop. That is, you might want to continue running the loop but stop processing the particular iteration. Then `continue` statement performs such an action.
26
47
27
-
-`if`
28
-
-`while`
48
+
Example.
49
+
```java
50
+
publicclassHelloWorld {
51
+
publicstaticvoidmain(String[] args) {
52
+
// Initially loop is set to run from 1-5
53
+
for(int i=1; i<=6; i++){
54
+
// terminate loop when i is 4.
55
+
if(i=4){
56
+
continue;
57
+
System.out.print(i);
58
+
}
59
+
}
60
+
}
61
+
}
62
+
63
+
```
64
+
Output: `1 2 3 5 6`
65
+
66
+
## **Break vs Continue**
67
+
Let's us see how these two jump statements are different from each other.
68
+
69
+
70
+
| break | continue |
71
+
| ----------------- | --------------- |
72
+
|The break statement is used to terminate the loop immediately. | The continue statement is used to skip the current iteration of the loop.|
73
+
| break keyword is used to indicate break statements in java programming. | continue keyword is used to indicate continue statement in java programming.|
74
+
| We can use a break with the switch statement. | The continue statement brings the next iteration early. |
75
+
| It stops the execution of the loop. | It does not stop the execution of the loop.|
76
+
77
+
78
+
79
+
## **Return Statement**
80
+
The `return` statement is used to explicitly return from a method. That is, it causes a program control to transfer back to the caller of the method.
81
+
It is used to **exit** from a method, with or without a value. Usage of **return keyword** as there exist two ways as listed below as follows:
82
+
83
+
-**Case 1:** Methods returning a value
84
+
-**Case 2:** Methods not returning a value
85
+
86
+
87
+
## **Methods returning a value**
88
+
```java
89
+
// Main method
90
+
classCodeExample {
91
+
// Method 1
92
+
publicstaticintsumFunction(inta, intb) {
93
+
int sum = a + b;
94
+
// Since return type of sunFunction method is integer so this method should return integer value
95
+
return sum;
96
+
}
97
+
98
+
// Main driver method
99
+
publicstaticvoidmain(String[] args){
100
+
int a =5;
101
+
int b =8;
102
+
// here ans variable will receive sum from sumFunction
103
+
int ans = sumFunction(a, b);
104
+
// print statement
105
+
System.out.println(ans);
106
+
}
107
+
}
108
+
109
+
```
110
+
Output: `13`
111
+
112
+
**Output explanation:** When we are calling a class CodeExample method that has **return sum** which returns the value of sum and that’s value gets displayed on the console.
113
+
114
+
## **Methods not returning a value**
115
+
For methods that do not return a value, `return` statement in Java can be skipped. Here there arise two cases when there is no value been returned by the user as listed below as follows:
116
+
- Method not using return statement in void function
117
+
- Methods with return type void
118
+
119
+
#### Method not using return statement in void function
120
+
```java
121
+
// Main method
122
+
classCodeExample {
123
+
// Method 1
124
+
publicstaticvoidsumFunction(inta, intb) {
125
+
int sum = a + b;
126
+
// Since return type of sunFunction method is void so this method should not return any value.
127
+
System.out.println(sum);
128
+
}
129
+
130
+
// Main driver method
131
+
publicstaticvoidmain(String[] args){
132
+
int a =5;
133
+
int b =8;
134
+
// Here, we will just call the function and the program will execute successfully.
135
+
sumFunction(a, b);
136
+
}
137
+
}
138
+
```
139
+
Output: `13`
140
+
141
+
#### Method with return type void
142
+
```java
143
+
// Main method
144
+
classCodeExample {
145
+
// Method 1
146
+
publicstaticvoiddemoFunction(intn) {
147
+
if(n<10) {
148
+
// return statement below(only using return statement and not returning anything)
149
+
// control exits the method if this condition(i.e, n<9) is true.
0 commit comments