Comparison of the use of break, continue, return statements in Java

Source: Internet
Author: User
Tags comparison terminates

This article mainly introduces the use of break, continue and return statements in Java, this paper summarizes the use of these three keywords in a very refreshing and concise language, and uses an example to compare the results, the need for friends can refer to the

The difference and connection between break, continue and return

In the process of software development, logic clarity is very important. The code specification is also very important. Often the details determine success or failure. When writing code, be sure to understand the role of language and the methods and scenarios used. Below to introduce the break, continue, return three differences and contact.

1. Break:

(1). Ends the current entire loop, executing the statement below the current loop. Ignores any other statements and cyclic condition tests in the loop body.

(2). Can only jump out of a layer of loops, if your cycle is nested loops, then you need to follow your nesting level, the gradual use of break to jump out. [Level by step]

(3). Break in the circulation body, forced to end the cycle of execution, that is, to end the entire cycle process, not to judge the condition of the execution cycle is set up, directly to the circular statement below the statement.

(4). When the break appears in a switch statement body in the loop body, its function is only to jump out of the switch statement body.

In general: The nearest principle to end the current whole cycle.

2. Return:

(1). Return exits from the current method and returns to the statement of the method in which it was invoked to continue execution.

(2). Return returns a value to the statement that invokes the method, and the data type of the returned value must be the same as the type of the return value in the declaration of the method.

(3). Return can also be followed without parameters, without parameters is returned empty, in fact, the main purpose is to interrupt the function execution, return to the call function.

3. Continue:

(1). Terminates the execution of this cycle, skipping the statements that have not yet been executed after the continue statement in the current cycle, and then making a judgment of the next cyclic condition.

(2). End the current loop, and make the next loop judgment.

(3). Terminates the current cycle process, but he does not jump out of the loop, but continues to judge the loop condition to execute the statement. He can only end a process in the loop, but cannot terminate the loop to continue.

The code is as follows:

Copy code code as follows:

public static void Test () {

for (int i = 0; i < i++) {

if (i = = 3) {

break;//

Continue;//i=3 the time to end this cycle, continue to execute the i=4 loop

return;//directly ends the entire function

}

SYSTEM.OUT.PRINTLN ("----" + i);

}

System.out.println (" -111--");

}

Attached Picture:

When it comes to return:

When it comes to the continue:

When it comes to the break:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.