Skip to content

Commit 9bcaa49

Browse files
committed
Updated jumps lesson
1 parent c1766cf commit 9bcaa49

File tree

1 file changed

+118
-236
lines changed

1 file changed

+118
-236
lines changed

lessons/jumps.md

Lines changed: 118 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -6,242 +6,16 @@ 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:
10-
- Conditional jumps
9+
Jumps statements are used to jump to a specific line in a program.
1110

12-
- Unconditional jumps
13-
14-
## **Conditional Jumps**
15-
16-
Conditional jumps are used to jump to a specific line in a program. It has two parameters, the line number to jump to and a condition. There are five types of conditional jumps:
17-
18-
- `if`
19-
20-
- `if-else`
21-
22-
- `nested-if`
23-
24-
- `if-else-if`
25-
26-
- `switch case`
27-
28-
## **if**
29-
`if` statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not. That is, if a certain condition is true then a block of statement is executed otherwise not.
11+
Java supports three jump statements.
12+
- `break`
13+
- `continue`
14+
- `return`
3015

31-
### Syntax:
32-
33-
```java
34-
if(condition)
35-
{
36-
// statements to excute if condition is true.
37-
}
38-
```
39-
Note: Take care of braces here. If we do not provide the curly braces ‘{‘ and ‘}’ after **if( condition )** then by default `if` statement will consider the very first statement to be inside its block.
40-
For ex:
41-
```java
42-
if(condition)
43-
statement1
44-
statement2
45-
statement3
46-
// Here if the condition is true, **if** block will consider only statement1 to be inside its block.
47-
```
48-
49-
Example.
50-
```java
51-
public class ifDemo {
52-
public static void main(String[] args) {
53-
int i = 10;
54-
if(i < 15)
55-
{
56-
System.out.println("10 is less than 15");
57-
}
58-
}
59-
}
60-
```
61-
Output: `10 is less than 15`
62-
63-
64-
## **if-else**
65-
66-
The `if` statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the `else` statement. We can use the `else` statement with `if` statement to execute a block of code when the condition is false.
67-
68-
### Syntax:
69-
```java
70-
if(condition)
71-
{
72-
// Executes this block if condition is true.
73-
}
74-
else
75-
{
76-
// Executes this block if condition is false.
77-
}
78-
```
79-
Example.
80-
```java
81-
public class ifelseDemo {
82-
public static void main(String[] args) {
83-
int i = 10;
84-
if(i < 15)
85-
{
86-
System.out.println("i is less than 15");
87-
}
88-
else
89-
{
90-
System.out.println("i is greater than 15");
91-
}
92-
}
93-
}
94-
```
95-
Output: `i is less than 15`
96-
97-
98-
## **nested-if**
99-
100-
Java allows us to nest `if` statements within `if` statements. That is, we can place an `if` statement inside another `if` statement.
101-
102-
### Syntax:
103-
104-
```java
105-
if (condition1)
106-
{
107-
// Executes when condition1 is true
108-
if (condition2)
109-
{
110-
// Executes when condition2 is true
111-
}
112-
}
113-
```
114-
Example.
115-
```java
116-
class NestedIfDemo{
117-
public class void Main(String args[]){
118-
int i = 10;
119-
if(i==10)
120-
{
121-
// First if statement
122-
if(i<15){
123-
System.out.println("i is less than 15");
124-
}
125-
if(i<12){
126-
System.out.println("i is less than 12 too");
127-
}
128-
else{
129-
System.out.println("i is greater than 15");
130-
}
131-
}
132-
}
133-
}
134-
```
135-
Output:
136-
`i is smaller than 15`
137-
`i is smaller than 12 too`
138-
139-
140-
## **if-else-if**
141-
Here, a user can decide among multiple options. The `if` statements are executed from the top down. As soon as one of the conditions controlling the` if` is true, the statement associated with that `if` is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final `else` statement will be executed.
142-
143-
### Syntax:
144-
145-
```java
146-
if (condition)
147-
statement;
148-
else if (condition)
149-
statement;
150-
.
151-
.
152-
else
153-
statement;
154-
```
155-
Example:
156-
```java
157-
class ifelseifDemo{
158-
public static void main(String args[])
159-
{
160-
int i = 20;
161-
162-
if (i == 10)
163-
System.out.println("i is 10");
164-
else if (i == 15)
165-
System.out.println("i is 15");
166-
else if (i == 20)
167-
System.out.println("i is 20");
168-
else
169-
System.out.println("i is not present");
170-
}
171-
}
172-
173-
```
174-
Output: `i is 20`
175-
176-
177-
178-
179-
## **switch-case**
180-
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
181-
182-
### Syntax:
183-
```java
184-
switch (expression)
185-
{
186-
case value1:
187-
statement1;
188-
break;
189-
case value2:
190-
statement2;
191-
break;
192-
.
193-
.
194-
case valueN:
195-
statementN;
196-
break;
197-
default:
198-
statementDefault;
199-
}
200-
```
201-
- Expression can be of type `byte`, `short`, `int`, `char` or an `enumeration`. Beginning with JDK7, expression can also be of type `String`.
202-
- Duplicate case values are not allowed.
203-
- The `default` statement is optional.
204-
- The `break` statement is used inside the switch to terminate a statement sequence.
205-
- The `break` statement is optional. If omitted, execution will continue on into the next case.
206-
207-
Example:
208-
```java
209-
class switchCaseDemo{
210-
public static Main(String args[]){
211-
int i = 9;
212-
switch(i)
213-
{
214-
case 0:
215-
System.out.println("i is zero.");
216-
break;
217-
case 1:
218-
System.out.println("i is one.");
219-
break;
220-
case 2:
221-
System.out.println("i is two.");
222-
break;
223-
case 3:
224-
System.out.println("i is three.");
225-
break;
226-
default:
227-
System.out.println("i is greater than 2.");
228-
}
229-
}
230-
}
231-
```
232-
Output: `i is greater than 2.`
233-
234-
235-
## **Unconditional Jumps**
236-
237-
Unconditional jumps are used to jump to a specific line in a program.
238-
239-
It has a single parameter, the line number to jump to. There are two types of unconditional jumps:
240-
241-
- `break`
242-
243-
- `continue`
244-
16+
These three statements transfer control to other part of the program.
17+
18+
Let's see one by one how it works.
24519

24620
## **break**
24721
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.
@@ -266,9 +40,9 @@ Output: `1 2 3`
26640

26741
In Java, `break` is majorly used for:
26842
- To exit a loop.
269-
- Terminate a sequence in a switch statement.
43+
- Terminate a sequence in a `switch` statement.
27044

271-
## **Continue**
45+
## **continue**
27246
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.
27347

27448
Example.
@@ -288,3 +62,111 @@ for(int i=1; i<=6; i++){
28862

28963
```
29064
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**
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+
{
101+
int a = 5;
102+
int b = 8;
103+
// here ans variable will receive sum from sumFunction
104+
int ans = sumFunction(a, b);
105+
// print statement
106+
System.out.println(ans);
107+
}
108+
}
109+
110+
```
111+
Output: `13`
112+
113+
**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.
114+
115+
## **Methods not returning a value**
116+
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:
117+
- Method not using return statement in void function
118+
- Methods with return type void
119+
120+
#### Method not using return statement in void function
121+
```java
122+
// Main method
123+
class CodeExample{
124+
// Method 1
125+
public static void sumFunction(int a, int b) {
126+
int sum = a + b;
127+
// Since return type of sunFunction method is void so this method should not return any value.
128+
System.out.println(sum);
129+
}
130+
131+
// Main driver method
132+
public static void main(String[] args)
133+
{
134+
int a = 5;
135+
int b = 8;
136+
// Here, we will just call the function and the program will execute successfully.
137+
sumFunction(a, b);
138+
}
139+
}
140+
```
141+
Output: `13`
142+
143+
#### Method with return type void
144+
```java
145+
// Main method
146+
class CodeExample{
147+
// Method 1
148+
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+
}
159+
}
160+
}
161+
162+
// Main driver method
163+
public static void main(String[] args)
164+
{
165+
int n = 8;
166+
// calling the function
167+
sumFunction(n);
168+
}
169+
}
170+
```
171+
Program executed successfully.
172+

0 commit comments

Comments
 (0)