The differences and connections between Break, Continue, and Return In the process of software development, the logic is very important. The specifications of code are also very important. Often details determine success or failure. When writing code, you must understand the role of language and the methods and scenes used. Let's introduce the differences and connections between Break, Continue, and Return.
1. Bream:
(1). Ending the current entire loop and execute the sentence below the current cycle. Ignore any other statements and cycle conditions in the cycle body.
(2). You can only jump out of a layer of cycle. If your cycle is an nested cycle, then you need to gradually use Break to jump out according to your nested level. [Judgment layer by layer, jump out gradually]
(3). Bream In the cycle, the execution of the cycle is forcibly ended, that is, the entire cycle process is ended, and it is not determined whether the conditions for the execution cycle are established, and the sentences below the cycle statement are directly turned.
(4). When Bream appears in the Switch statement in the cycle body, its role is only to jump out of the Switch statement body.
Generally speaking: the principle of nearby ending the current entire loop.
2. Return:
(1 )Rturn exit from the current method, return to the statement of the call method, and continue to execute.
(2) Return a statement that calls a value that calls the method. The data type of the return value must be consistent with the type of the return value in the statement of the method.
(3). You can also have no parameters behind Return. Without parameters is to return empty. In fact, the main purpose is to use it to interrupt the function execution and return the call function.
3. Continue:
(1). Termination of the execution of this cycle, that is, the statement that has not been executed after the Continue statement in the current cycle, and then judge the next cycle conditions.
(2). Ending the current cycle and the next cycle judgment.
(3). Termid the current cycle process, but he does not jump out of the cycle, but continues to judge the loop conditions to perform statements. He can only end the process in the cycle, but he cannot terminate the cycle to continue.
The code is as follows:
Copy code code as follows:
public static void test () {
for (int i = 0; i <10; i ++) {
if (i == 3) {
// break; //
// Continue; // I = 3 End the cycle when I = 3, and continue to execute the loop of i = 4
Return; // directly end the entire function
}
System.out.println ("----" + i);
}
System.out.println ("-111--");
}
Attach picture:
When it is RETURN:
When it is constinue:
When it is Break: