- Return is the function that exits the loop body, which is equivalent to ending the method.
- The function of break is to end the loop, jump out of the loop body, and execute the subsequent program.
- 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