What are the java jump statements?
This article will explain in detail what are the java jump statements, the quality of the article content is high, so Xiaobian shares it for everyone to make a reference, I hope you have a certain understanding of relevant knowledge after reading this article.
Java supports three jump statements: break, continue and return. These statements transfer control to other parts of the program.
Each is discussed below.
Note: In addition to the jump statements discussed here, Java supports another way to change the flow of your program execution: through exception handling. Exception handling provides a structured way for your program to catch and handle run-time errors. It is controlled by five keywords: try, catch, throw, throws, and finally. Essentially, exception handling allows your program to make a nonlocal branch jump. Because exception handling is a big topic, we'll devote chapter 10 to it.
5.3.1 Using the break statement
In Java, break statements have three roles. First, you've seen that in switch statements, it's used to terminate a sequence of statements. Second, it can be used to exit a loop. Third, it can be used as an "advanced" goto statement. The last two uses are explained below.
Use break to exit the loop
You can use the break statement to force a loop out directly, ignoring any other statements in the loop body and the loop's conditional tests. When a break statement is encountered in the loop, the loop is terminated and program control resumes with the statement following the loop. Here is a simple example:
// Using break to exit a loop.
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i