The importance of break, continue, and return in programs

Source: Internet
Author: User

First, the break statement is usually used in loop statements and switch statements. When the break statement is used in do-while, for, and while loop statements, the program can terminate the loop and execute the statements following the loop, generally, the break statement is always associated with the if statement, that is, when the condition is met, it jumps out of the loop.

Example: main () {int I = 0; char c; while (1)/* Set loop */{c = '\ 0 '; /* assign initial values to the variable */while (c! = 13 & c! = 27)/* receive characters on the keyboard until press ENTER or Esc key */{c = getch (); printf ("% c \ n", c );} if (c = 27) break;/* determines that if you press Esc, The loop will exit */I ++; printf ("The No. is % d \ n ", I);} printf (" The end ");}

Note:
1) The break statement does not work for the if-else Condition Statement. Www.2cto.com

2) in a multi-tier loop, a break statement only jumps one layer outward.

Second, the function of the continue statement is to skip the remaining statements in the loop and forcibly execute the next loop. The continue statement is only used in loop bodies such as for, while, and do-while. It is often used together with the if Condition Statement to accelerate the loop. Example: main () {char c; while (c! = 13)/* loop */{c = getch (); if (c = 0X1B) continue; /* If you press the Esc key and do not output it, perform the next loop */printf ("% c \ n", c);}. In fact, continue skips one loop and subsequent statements, perform the next loop.

Third, the return Statement returns the value of the function to the main function. For example: int max (int a, int B) {if (a> B) return a; else return B;} This function returns the maximum value in a and B; the return Statement is generally in the form of a return expression or a return (expression)

 

From chaojie2009

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.