The difference between break and continue

Source: Internet
Author: User
Tags terminates

Break and continue are used to control the loop structure, mainly to stop the loop.

1.break

Sometimes we want to terminate the loop when a certain condition occurs, rather than wait for the loop condition to be false.

This is what we can do with break. Break is used to completely end a loop and jump out of the loop body to execute the statement following the loop.

2.continue

Continue and break are a bit similar, except that continue terminates the loop, and then executes the subsequent loop, and break terminates the loop completely.

It can be understood that continue is skipping the remaining statements in the secondary loop, performing the next loop.

Example:

[CPP]View PlainCopy 
  1. #include <stdio.h>
  2. void Main ()
  3. {
  4. int sum,i;
  5. sum=0;
  6. For (i=1;i<=100;i++)
  7. {
  8. Sum=sum+i;
  9. if (i==2)
  10. {
  11. printf ("executive Continue");
  12. continue;
  13. }
  14. if (i==4)
  15. {
  16. printf ("Execution break");
  17. Break ;
  18. }
  19. printf ("sum=%d", sum);
  20. }
  21. printf ("loop over");
  22. }



The result of this operation is: Sum=1;

implementation of continue;

sum=6;

Execution break Loop End

Run end.

Original link: http://blog.csdn.net/abc5382334/article/details/17308375

The difference between break and continue

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.