The difference between return, break, and continue in a while loop

Source: Internet
Author: User

    1. Return is the function that exits the loop body, which is equivalent to ending the method.
    2. The function of break is to end the loop, jump out of the loop body, and execute the subsequent program.
    3. The role of continue is to end the cycle and carry out the next cycle;
      The following procedures are used to illustrate:
#include <iostream>using namespace STD;voidTest1 (int&i) { while(i--) {if(i<5)   {return;//When i=4, exit the function}} i=i+1;}voidTest2 (int&i) { while(i--) {if(i<5)   { Break;//When i=4, exit the while loop and proceed down i=i+1}} i=i+1;}voidTest3 (int&i) { while(i--) {if(i<5)   {Continue;/// when i=4, exit this cycle and continue with the next while Loop}} i=i+1;}intMain () {intA,b,c; A=Ten; b=Ten; C=Ten; Test1 (a);cout<<a<<endl;//result is 4Test2 (b);cout<<b<<endl;//result is 5Test3 (c);cout<<c<<endl;//result is 0System"Pause");return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between return, break, and continue in a while loop

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.