Usage of return,break,continue in C + +

Source: Internet
Author: User

References: 51315835

73770838

The return statement is used to end the currently executing function and return control to the function that called the function.    The return statement has two forms: return value and no return value, return; return x;

First: For use without return values, return statements without return values can only be used to return a function with a type void, and the return statement is intended to cause a forced end of the function, which is similar to the effect of a break statement in a looping structure.

Second: For the use of the return value, it cannot be used for a function of type void, otherwise an error

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, and the data type of the return value must match the type of the return value in the declaration of the method. (3) return back can also be without parameters, without parameters is returned empty, in fact, the main purpose is to interrupt the function execution, return to the calling function. 2. The effect of the break statement(1) break in the loop body, forcibly end the execution of the loop, that is, the end of the entire cycle, not to determine whether the condition of the execution loop is established, directly to the statement below the circular 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. the role of 3.continue statementsterminates the execution of this loop, that is, the statement that has not been executed after the continue statement in the current loop is skipped, and then the next cycle condition is judged. below you can see the specific example, may be more clear:

  1. #include <stdio.h>
  2. int main ()
  3. {
  4. int i = 5,n = 0;
  5. while (i--)
  6. {
  7. if (i = = 3)
  8. Return
  9. Break
  10. Continue
  11. else if (i = = 1)
  12. n = 6;
  13. }
  14. n = n + 5;
  15. printf ("i=%d\n", I);
  16. printf ("n=%d\n", N);
  17. return 0;
  18. }
When you run continue , the result is:
    1. I=-1
    2. n=11

When you run break, the result is:

    1. I=3
    2. N=5
When you run return , there is no result, stating that the main function was brought out when I==3 was executed, and the following statement will not be executed (no print operation will be performed)

Usage of return,break,continue in C + +

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.