Skip to content

Commit c1766cf

Browse files
committed
Updated Jump Statements in Java
1 parent f124895 commit c1766cf

File tree

1 file changed

+272
-10
lines changed

1 file changed

+272
-10
lines changed

lessons/jumps.md

Lines changed: 272 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,284 @@ description: "Learn Java"
77
---
88

99
Jumps statements are used to jump to a specific line in a program. There are two type of jumps:
10+
- Conditional jumps
1011

1112
- Unconditional jumps
12-
- Conditional jumps
1313

14-
### **Unconditional Jumps**
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.
30+
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**
15236

16237
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:
18238

19-
- `goto`
20-
- `continue`
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+
245+
246+
## **break**
247+
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.
248+
249+
Example.
250+
```java
251+
public class HelloWorld {
252+
public static void main(String[] args) {
253+
// Initially loop is set to run from 1-5
254+
for(int i=1; i<=5; i++){
255+
// terminate loop when i is 4.
256+
if(i=4){
257+
break;
258+
System.out.print(i);
259+
}
260+
}
261+
}
262+
}
263+
264+
```
265+
Output: `1 2 3`
266+
267+
In Java, `break` is majorly used for:
268+
- To exit a loop.
269+
- Terminate a sequence in a switch statement.
21270

22-
### **Conditional Jumps**
271+
## **Continue**
272+
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.
23273

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:
274+
Example.
275+
```java
276+
public class HelloWorld {
277+
public static void main(String[] args) {
278+
// Initially loop is set to run from 1-5
279+
for(int i=1; i<=6; i++){
280+
// terminate loop when i is 4.
281+
if(i=4){
282+
continue;
283+
System.out.print(i);
284+
}
285+
}
286+
}
287+
}
26288

27-
- `if`
28-
- `while`
289+
```
290+
Output: `1 2 3 5 6`

0 commit comments

Comments
 (0)