Home > Java > javaTutorial > What is the function of break keyword in Java?

What is the function of break keyword in Java?

王林
Release: 2023-04-23 10:13:06
forward
3098 people have browsed it

Explanation

1. The function of break is to jump out of the current loop block (for, while, dowhile) or program block (switch).

2. The function of the loop block is to jump out of the loop body currently in the loop. The function of the program block is to interrupt and compare the next case condition.

Use break in the switch statement to terminate the switch statement.

When break is used in a loop, jump out of the loop.

It makes no sense to use break elsewhere.

Example

int sum = 0;
int i;
for (i = 1; i <= 100; i++) {
    if (i == 77) {
        break;
    }
    sum += i;
}
System.out.println("1累计到76的结果:" + sum + "  i:" + i);
Copy after login

After executing break, exit the for loop directly and execute the print statement.

When i is 77, break is executed, but i is not executed at this time.

What is Java

Java is an object-oriented programming language that can write desktop applications, Web applications, distributed systems and embedded system applications.

The above is the detailed content of What is the function of break keyword in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template