Skip to content

Commit 9d3c2b6

Browse files
authored
Merge pull request #579 from javaistic/add-break-statement-docs
Add break statement docs
2 parents 967cd25 + a96f414 commit 9d3c2b6

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export const documentationNav = {
2424
pages['for-loop'],
2525
pages['enhanced-for-loop'],
2626
pages['while-and-do-while-loop'],
27+
pages['break-statement'],
2728
],
2829
}

src/pages/docs/break-statement.mdx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Java break Statement
3+
description: In this tutorial, we will learn how to use the Java break statement to terminate a loop or switch statement.
4+
---
5+
6+
While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.
7+
8+
In such cases, `break` and `continue` statements are used. You will learn about the Java continue statement in the next tutorial.
9+
10+
The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop.
11+
12+
It is almost always used with decision-making statements (Java if...else Statement).
13+
14+
Here is the syntax of the break statement in Java:
15+
16+
```java
17+
break;
18+
```
19+
20+
import { List, ListItemGood } from '@/components/List'
21+
import { TipInfo } from '@/components/Tip'
22+
23+
24+
## Example 1: Java break statement
25+
26+
Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is:
27+
28+
```java
29+
class Test {
30+
public static void main(String[] args) {
31+
32+
// for loop
33+
for (int i = 1; i <= 10; ++i) {
34+
35+
// if the value of i is 5 the loop terminates
36+
if (i == 5) {
37+
break;
38+
}
39+
System.out.println(i);
40+
}
41+
}
42+
}
43+
```
44+
#### Output
45+
46+
```text
47+
1
48+
2
49+
3
50+
4
51+
```
52+
53+
In the above program, we are using the `for` loop to print the value of i in each iteration. To know how for loop works, visit the Java `for` loop. Here, notice the statement,
54+
55+
```java
56+
if (i == 5) {
57+
break;
58+
}
59+
```
60+
This means when the value of `i` is equal to 5, the loop terminates. Hence we get the output with values less than 5 only.
61+
62+
63+

0 commit comments

Comments
 (0)