Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/navs/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export const documentationNav = {
pages['for-loop'],
pages['enhanced-for-loop'],
pages['while-and-do-while-loop'],
pages['break-statement'],
],
}
63 changes: 63 additions & 0 deletions src/pages/docs/break-statement.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: Java break Statement
description: In this tutorial, we will learn how to use the Java break statement to terminate a loop or switch statement.
---

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.

In such cases, `break` and `continue` statements are used. You will learn about the Java continue statement in the next tutorial.

The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop.

It is almost always used with decision-making statements (Java if...else Statement).

Here is the syntax of the break statement in Java:

```java
break;
```

import { List, ListItemGood } from '@/components/List'
import { TipInfo } from '@/components/Tip'


## Example 1: Java break statement

Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is:

```java
class Test {
public static void main(String[] args) {

// for loop
for (int i = 1; i <= 10; ++i) {

// if the value of i is 5 the loop terminates
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
```
#### Output

```text
1
2
3
4
```

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,

```java
if (i == 5) {
break;
}
```
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.