Return, continue, break usage and difference summary, returncontinue

Source: Internet
Author: User

Return, continue, break usage and difference summary, returncontinue

1. Functions of return statements

(1) return exits from the current method, returns to the statement of the method called, and continues execution.
(2) return returns a value to the statement that calls this method. The data type of the returned value must be the same as the type of the returned value in the method declaration. You can use forced type conversion to ensure that the data type is the same.
(3) return when void is used to declare that the return type is null in the method description, this format should be used without returning any value.

Example: return is a method that jumps out of the entire method.

Public static void GetNum () {for (int I = 0; I <3; I ++) {if (I = 2) {return;} Console. writeLine (I) ;}// when this method is called, the result is 0 1, indicating that when I = 2, the code after return is not executed, and the method body jumps out directly. (Note: no matter how many layers of loops are nested, return directly jumps out of the entire method body)View Code

 

2. Roles of the break statement
(1) The break statement can only be used in the loop body and switch statement body.
(2) When the break appears in the switch statement body of the loop body, it only jumps out of the switch statement body.
(3) When the break appears in the loop body but is not in the switch statement body, it jumps out of the loop body at the current layer after the break is executed.
(4) In the loop structure, the break statement is applied to make the process jump out of the cycle body of the Current layer, so as to end the cycle of the current layer in advance.

Example: break ends the entire loop body.

For (int I = 0; I <10; I ++) {if (I = 3) {break;} Console. writeLine (I);} // The result is that the entire for loop is exited when the output is 0 1 2. That is, when I = 3, the code after the break in the loop body is not executed, end the entire loop body of the Current layer (Note: exit the current layer loop when nested loops)View Code

 

3. Functions of the continue statement

(1) The general form of the continue statement is contonue;
(2) its function is to end this cycle, that is, to skip the remaining statements in the cycle body that have not been executed, and then judge the conditions of the cycle again.
(3) Note: The execution of the continue statement does not terminate the entire loop. In the while and do-while loops, The continue statement causes the process to jump directly to the test section of the loop control condition, and then determines whether the loop continues.
(4) In a for loop, when a continue is encountered, skip the remainder statement in the loop body and evaluate the value of "expression 3" in the for statement, then perform the conditional test of expression 2, and determine whether to execute the for Loop Based on the value of expression 2. In the loop body, no matter what statement the continue is, it will be executed according to the above function, which is different from break.

Example: continue ends a single loop

For (int I = 0; I <10; I ++) {if (I = 3) {continue;} Console. writeLine (I);} // The result is: 0 1 2 4 5 6 7 8 9. We can see that he only does not output 3 because he has ended this loop, that is, when I = 3, the code after the continue in the loop body is directly executed for the next loop without executing it.View Code

 

Related Article

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.