The difference between Break,continue,return and exit

Source: Internet
Author: User

1. Break

The use of the break statement is mainly the switch statement and the loop structure.

Use the break statement in the loop structure to exit the loop and then execute the first statement below the loop structure.

If a break statement is used in a multiple nested loop, when the break statement is executed, the loop structure in which it resides is exited, and the outer layer loop has no effect.

If a switch statement is in the loop structure and a break statement is used in the switch statement, when the break statement in the switch statement is executed, only the switch statement is exited, and the outer loop structure is not exited.

2, continue

The continue statement ends the loop in the most special way, because it does not really exit the loop, but only ends the execution of the loop body.

In the For loop, the expression 1 is executed first (note that expression 1 executes only once in the entire loop), then the expression 2 is executed, and if the condition is met, the loop body is executed, and if the continue statement is executed in the loop body, it jumps to the expression 3 and executes the next loop. Execute expression 2 to see if the condition is met;

In the while loop, if the continue statement is executed, it jumps directly to the expression and begins the Next loop judgment;

If the continue statement is executed in the Do While loop body, it jumps to the next loop judgment at the expression.

3. Return

If you encounter a return statement in a program, exit the function's execution, return to the function's call, and, if it is the main () function, end the entire program run. If executed in a custom function, then return to the function's call to continue execution after the return is executed.

4. Exit ()

The most important difference between the exit () function and the return statement is that calling the exit () function will end the current process while deleting the memory space occupied by the child process and passing the return information to the parent process. When the argument in exit () is 0 o'clock, indicating normal exit, the other return value indicates an abnormal exit, the exit () function means that the process is finished, and return represents the return of the call stack, which returns the function value and exits the currently executing function body, returning to the function's call, in main ( ) function, return n and exit (n) are equivalent.

The difference between Break,continue,return and exit

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.