Skip to content

Commit 332d2eb

Browse files
committed
Updated Jumps Lesson
1 parent 9bcaa49 commit 332d2eb

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

lessons/jumps.md

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ Java supports three jump statements.
1717

1818
Let's see one by one how it works.
1919

20-
## **break**
20+
## **Break statements**
2121
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.
2222

2323
Example.
2424
```java
2525
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-
}
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+
}
3636
}
3737

3838
```
@@ -42,28 +42,28 @@ In Java, `break` is majorly used for:
4242
- To exit a loop.
4343
- Terminate a sequence in a `switch` statement.
4444

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

4848
Example.
4949
```java
5050
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);
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+
}
5859
}
5960
}
60-
}
6161
}
6262

6363
```
6464
Output: `1 2 3 5 6`
6565

66-
## **break vs continue**
66+
## **Break vs Continue**
6767
Let's us see how these two jump statements are different from each other.
6868

6969

@@ -76,7 +76,7 @@ Let's us see how these two jump statements are different from each other.
7676

7777

7878

79-
## **return**
79+
## **Return Statement**
8080
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.
8181
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:
8282

@@ -87,17 +87,16 @@ It is used to **exit** from a method, with or without a value. Usage of **return
8787
## **Methods returning a value**
8888
```java
8989
// Main method
90-
class CodeExample{
91-
// Method 1
90+
class CodeExample {
91+
// Method 1
9292
public static int sumFunction(int a, int b) {
9393
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
9595
return sum;
9696
}
9797

9898
// Main driver method
99-
public static void main(String[] args)
100-
{
99+
public static void main(String[] args){
101100
int a = 5;
102101
int b = 8;
103102
// 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
120119
#### Method not using return statement in void function
121120
```java
122121
// Main method
123-
class CodeExample{
124-
// Method 1
122+
class CodeExample {
123+
// Method 1
125124
public static void sumFunction(int a, int b) {
126125
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.
128127
System.out.println(sum);
129128
}
130129

131130
// Main driver method
132-
public static void main(String[] args)
133-
{
131+
public static void main(String[] args){
134132
int a = 5;
135133
int b = 8;
136134
// Here, we will just call the function and the program will execute successfully.
137-
sumFunction(a, b);
135+
sumFunction(a, b);
138136
}
139137
}
140138
```
@@ -143,30 +141,26 @@ Output: `13`
143141
#### Method with return type void
144142
```java
145143
// Main method
146-
class CodeExample{
147-
// Method 1
144+
class CodeExample {
145+
// Method 1
148146
public static void demoFunction(int n) {
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.
150+
return;
151+
}
152+
else{
153+
n++;
159154
}
160-
}
155+
}
156+
}
161157

162158
// Main driver method
163-
public static void main(String[] args)
164-
{
165-
int n = 8;
166-
// calling the function
159+
public static void main(String[] args){
160+
int n = 8;
161+
// calling the function
167162
sumFunction(n);
168163
}
169164
}
170165
```
171166
Program executed successfully.
172-

0 commit comments

Comments
 (0)