What is continue; in a loop?

0 Usama tariq · April 4, 2015
Can someone please explain the continue statement in a loop and its purpose.

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · April 4, 2015
The continue statement is like the break statement a keyword for a loop.

break:
break jumps out of a loop immediatelly

continue:
beginns a NEW loop run immediatelly
e.g.:

int main(){
     for(int i=0; i <= 5; i++){
          cout << i;
          if(i <= 3){
               continue;
          }
          cout << "is bigger than 3";
     }
}

output will be:
0
1
2
3
4 is bigger than 3
5 is bigger than 3


sorry for bad code format but code tags seem to be broken
0 Mathias Frits Rørvik · April 4, 2015
continue goes to the next iteration of the loop. =)

Sometimes you have some code inside a for loop which you don't want to run unless it meets a certain condition, with a continue you can skip a round. Just don't worry about it that much, you'll eventually find use of it when you become more experience.

Don't give up, and keep coding!
0 Milan Obrenovic · April 4, 2015

sorry for bad code format but code tags seem to be broken


Lamest excuse ever.

Here's the formatted code:

#include <iostream>
using namespace std;

int main()
{
    for (int i = 0; i <= 5; i++)
    {
        cout << i << endl;

        if (i <= 3)
            continue;

        cout << "is bigger than 3" << endl;
    }

    return 0;
}
0 Dol Lod · April 4, 2015
Use continue only if you want to move on to the next iteration without executing any code located in the body of the for loop or while loop. 

In common code, it is rare to need to actually use continue. 

In this example, it would be much more efficient to instead change that last part to printing out only if i is greater than 3 as opposed to using a continue if i<=3. 
  • 1

C++

107,113 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator