Return, summary of usage and difference of continue,break

Source: Internet
Author: User

1. Function of Return statement

(1) Return exits from the current method and returns to the statement of the method that is called to continue execution.
(2) return returns a value to the statement that invokes the method, the data type of the return value must be the same as the type of the return value in the declaration of the method, and you can use the coercion type conversion to be consistent with the data type.
(3) Return when the return type of void declaration in the method description is empty, this format should be used and no value is returned.

Example: Return is out of the way

 Public Static void Getnum () {      for (int03; i++)      {            if (i = =2)            {                return;             }             Console.WriteLine (i);        }} // when this method is called, the result is 0 1 indicating that the code after the method body return does not execute when i==2, and jumps out of the method body directly. (Note: No matter how many layers of nested loops encounter return directly out of the entire method body)
View Code

2. Function of break statement
(1) The break statement can be used only within the body of the loop and in the switch statement.
(2) When break appears in the body of the switch statement in the loop body, its function is simply to jump out of the switch statement body.
(3) When break appears in the loop body, but not in the switch statement body, then after the break, the body of the loop is out of the layer.
(4) In the loop structure, the break statement is applied to make the process jump out of the loop body of this layer, thus ending this layer loop prematurely.

Example: Break is the end of the entire loop body

 for (int0; i++)     {if (i==3)     {            break;     }      Console.WriteLine (i);} // The result is that the output 0 1 2 exits the entire for loop, that is, the code after the body break in the loop does not execute when i==3, ending the entire loop body of this layer (note: Exiting this layer loop when nesting loops)
View Code

3. Continue statement function

(1) The general form of the continue statement is: Contonue;
(2) Its function is to end this cycle, that is, the remainder of the loop is skipped the remaining outstanding statements, and then once again the condition of the loop to determine.
(3) Note: Executing 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 portion of the loop control condition, and then decides whether the loop continues.
(4) in the For loop, after encountering continue, skips the remaining statements in the loop body, evaluates expression 3 in the For statement, then evaluates the conditional test for expression 2, and finally determines whether the for loop executes based on the value of expression 2. In the loop body, regardless of the statement component in which the continue is used, it is performed as described above, which differs from break.

Example: Continue end single cycle

   for (int0; i++)  {        if (i==3)        {             continue;         }         Console.WriteLine (i);    } // The result is: 0 1 2 4 5 6 7 8 9 Visible He just does not output 3, because he ended the cycle, that is, when the i==3 loop in the body continue the code behind does not perform the next loop directly
View Code

Return, summary of usage and difference of continue,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.