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
Copy file name to clipboardExpand all lines: lessons/jumps.md
+46-52Lines changed: 46 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,22 +17,22 @@ Java supports three jump statements.
17
17
18
18
Let's see one by one how it works.
19
19
20
-
## **break**
20
+
## **Break statements**
21
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.
22
22
23
23
Example.
24
24
```java
25
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
-
}
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
36
}
37
37
38
38
```
@@ -42,28 +42,28 @@ In Java, `break` is majorly used for:
42
42
- To exit a loop.
43
43
- Terminate a sequence in a `switch` statement.
44
44
45
-
## **continue**
46
-
Sometimes you doesn't want to excute 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.
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.
47
47
48
48
Example.
49
49
```java
50
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);
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
+
}
58
59
}
59
60
}
60
-
}
61
61
}
62
62
63
63
```
64
64
Output: `1 2 3 5 6`
65
65
66
-
## **break vs continue**
66
+
## **Break vs Continue**
67
67
Let's us see how these two jump statements are different from each other.
68
68
69
69
@@ -76,7 +76,7 @@ Let's us see how these two jump statements are different from each other.
76
76
77
77
78
78
79
-
## **return**
79
+
## **Return Statement**
80
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
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
82
@@ -87,17 +87,16 @@ It is used to **exit** from a method, with or without a value. Usage of **return
87
87
## **Methods returning a value**
88
88
```java
89
89
// Main method
90
-
classCodeExample{
91
-
// Method 1
90
+
classCodeExample{
91
+
// Method 1
92
92
publicstaticintsumFunction(inta, intb) {
93
93
int sum = a + b;
94
-
// Since return type of sunFunction method is integer so this method should return integer value
94
+
// Since return type of sunFunction method is integer so this method should return integer value
95
95
return sum;
96
96
}
97
97
98
98
// Main driver method
99
-
publicstaticvoidmain(String[] args)
100
-
{
99
+
publicstaticvoidmain(String[] args){
101
100
int a =5;
102
101
int b =8;
103
102
// here ans variable will receive sum from sumFunction
@@ -120,21 +119,20 @@ For methods that do not return a value, `return` statement in Java can be skippe
120
119
#### Method not using return statement in void function
121
120
```java
122
121
// Main method
123
-
classCodeExample{
124
-
// Method 1
122
+
classCodeExample{
123
+
// Method 1
125
124
publicstaticvoidsumFunction(inta, intb) {
126
125
int sum = a + b;
127
-
// Since return type of sunFunction method is void so this method should not return any value.
126
+
// Since return type of sunFunction method is void so this method should not return any value.
128
127
System.out.println(sum);
129
128
}
130
129
131
130
// Main driver method
132
-
publicstaticvoidmain(String[] args)
133
-
{
131
+
publicstaticvoidmain(String[] args){
134
132
int a =5;
135
133
int b =8;
136
134
// Here, we will just call the function and the program will execute successfully.
137
-
sumFunction(a, b);
135
+
sumFunction(a, b);
138
136
}
139
137
}
140
138
```
@@ -143,30 +141,26 @@ Output: `13`
143
141
#### Method with return type void
144
142
```java
145
143
// Main method
146
-
classCodeExample{
147
-
// Method 1
144
+
classCodeExample{
145
+
// Method 1
148
146
publicstaticvoiddemoFunction(intn) {
149
-
if(n<10)
150
-
{
151
-
// return statement below(only using return statement and not returning anything)
152
-
// control exits the method if this condition(i.e, n<9) is true.
153
-
return;
154
-
}
155
-
else
156
-
{
157
-
n++;
158
-
}
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