The difference between break, continue, return and exit in a circular structure

Source: Internet
Author: User
Tags exit in

1. break

The use of a break statement is mainly a switch statement and a looping structure. Use the break statement in a loop structure, and if you execute a break statement, exit the loop, and then execute the first statement under the loop structure. If you use a break statement in a multiple nested loop, when the break statement is executed, the exit is the loop structure in which it is located, and the external loop has no effect. If there is a switch statement 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. Figure 3-17 allows the reader to understand the use of the break statement intuitively.

(Click to view larger image) Figure 3-17 Break statement

2. Continue

The continue statement is the most special of the 5 end loops because it does not actually exit the loop, but rather ends the execution of this loop, so be aware of this when using continue. Figure 3-18 is the use of continue statements in various loop structures.

In the For loop, the expression 1 is executed first (note that expression 1 executes only once in the loop), then execute the expression 2, if the condition is met, then the execution loop body, if the continue statement is executed in the loop body, jumps to expression 3 to execute, then the next loop, Executes the expression 2 to see if the condition is met, and in the while loop, if the continue statement is executed, jump directly to the expression and start the next loop judgment; If the continue statement is executed in the Do-while loop body, Then jump to the expression to make the next loop judgment, which has been validated before.
(Click to view larger image) Figure 3

3. Return Statement

If you encounter a return statement in your program, the code exits the execution of the function, returns to the call to the function, and, if it is the main () function, ends the operation of the entire program. Figure 3-20 is used for the return statement.

(Click to view larger image) 3-20 return statement

If executed in a custom function, the return is returned to the function's call to continue execution.

4. Exit () function

The biggest difference between the exit () function and the return statement is that calling the exit () function ends the current process, deleting the memory space occupied by the child process, and passing the information back to the parent process. When the parameter in exit () is 0 o'clock, indicates a normal exit, the other return value indicates an abnormal exit, the execution of the exit () function means the end of the process, and return only means that the call stack returns, its function is to return the function value, and exits the currently executing function body, and returns to the function's call where the main ( function, return n and exit (n) are equivalent. Figure 3-21 is used for the exit () function.

Next, a simple comparison between the return statement and the exit () function is made by two pieces of code to see the use of the return statement first.

#include  <stdio.h>  #include  <stdlib.h>  int print ()   {       int n;       n = 0;       printf ("Use return to end loop \ n");       while (1)       {           if (9==n)               return  n;           n++;       }       return 0;  }     Void main (void)   {      int ret;     & NBSP;&NBSP;PRINTF (before calling the print () function \ n);       ret = print ();       printf ("The return value of the print () function ret=%d\n", ret);  

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.