Skip to content

Commit abdc27c

Browse files
authored
Merge pull request #32 from Ayushi7456/Jumps_Ayushi
Updated Jump Statements Lesson in Java
2 parents 824ba75 + 27cbf4d commit abdc27c

File tree

1 file changed

+148
-13
lines changed

1 file changed

+148
-13
lines changed

lessons/jumps.md

Lines changed: 148 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,158 @@ section: "Learn Java"
66
description: "Learn Java"
77
---
88

9-
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.
1010

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.
1319

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.
1522

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+
public class HelloWorld {
26+
public static void main(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+
}
1837

19-
- `goto`
20-
- `continue`
38+
```
39+
Output: `1 2 3`
2140

22-
### **Conditional Jumps**
41+
In Java, `break` is majorly used for:
42+
- To exit a loop.
43+
- Terminate a sequence in a `switch` statement.
2344

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.
2647

27-
- `if`
28-
- `while`
48+
Example.
49+
```java
50+
public class HelloWorld {
51+
public static void main(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+
class CodeExample {
91+
// Method 1
92+
public static int sumFunction(int a, int b) {
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+
public static void main(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+
class CodeExample {
123+
// Method 1
124+
public static void sumFunction(int a, int b) {
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+
public static void main(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+
class CodeExample {
145+
// Method 1
146+
public static void demoFunction(int n) {
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.
150+
return;
151+
} else {
152+
n++;
153+
}
154+
}
155+
// Main driver method
156+
public static void main(String[] args){
157+
int n = 8;
158+
// calling the function
159+
sumFunction(n);
160+
}
161+
}
162+
```
163+
Program executed successfully.

0 commit comments

Comments
 (0)