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

What is the function of break keyword in Java?

王林
王林forward
2023-04-23 10:13:063096browse

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);

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete