C++ introductory tutorial — 8 loop control statements

Loop control statements are usually used in conjunction with if. Use an if condition to break the loop (break)/ skip the loop (continue)

Example:

#include <iostream>
using namespace std;
 
int main (a)
{
   int i;
   for(i=1; i<=5; i++) {
      if(i==3) continue; // Skip this loop if it is 3
	  else cout<<i<<endl;
   }
 
   return 0;
}
Copy the code

Running results:

#include <iostream>
using namespace std;
 
int main (a)
{
   int i;
   for(i=1; i<=5; i++) {
      if(i==3) break; // If it is 3, break the loop
	  else cout<<i<<endl;
   }
 
   return 0;
}
Copy the code

Running results:

We can copy the code to our own compiler and analyze it carefully.