How to exit an iteration (for loop)

Hey guys,
I want to know if there is any keyword that can be used to exit an iteration, but not the entire loop.. for example if i want to print the numbers from 1 to 10, but exclude 6:

1
2
3
4
5
for(int i=1; i<=10; i++){
   if(i == 6)
      //what keyword could be used here to go to the next iteration?

   cout<< i << ", ";


thanks!
You can use the continue; keyword. Just be careful with its use as it can cause many infinite loop problems. It also forces the program flow to jump around a little which many people say is bad for readability.

The better bet might just be to only print if(i != 6).
@BlackSheep I am not understanding why you are so against use of 'continue'. Why would it cause an infinite loop? How does it cause program flow to jump around? It seems very obvious to me because you always know it is going to go to the next iteration of the closest enclosing loop. As for infinite loop issues, you have to be doing something wrong.
I for one, find the use of continue; very useful!

And as for infinite loops, I've never come across one using continue.

Take the following, for instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
	for(int i = 0; i < 10; i++ )
	{
		if( i > 6 )
			continue;

		cout << "i is now: " << i << '\n';
	}

	return 0;
}


This could never run an infinite loop without the value of i not meeting the condition. And probably some random unforeseen abnormality! lol

If anything, this program will run quicker that not having a continue;

With continuing this loop, you don't have to read/check any statements underneath.
Lynx876 wrote:
If anything, this program will run quicker that not having a continue;

With continuing this loop, you don't have to read/check any statements underneath.
This is not true.
I guess I was a little too drastic with the infinite loops, but it can cause problems for beginners, especially with while loops:
1
2
3
4
5
6
while(i < 10)
{
     if(i == 6) continue;
     std::cout<<i<<std::endl;
     ++i;
}


And the execution flow jumping around is just something I mentioned because there's always a way to accomplish the same thing using conditional statements, which gives a more "natural" feel to the program flow. (In my opinion)

I guess I shouldn't have made it seem like such a concrete fact. Just my two cents.
Last edited on
LB wrote:
Lynx876 wrote:
If anything, this program will run quicker that not having a continue;

With continuing this loop, you don't have to read/check any statements underneath.
This is not true.


How come? Say you have quite a bit of code under the conditional, continue. If this code doesn't have to be executed, surely it's faster?

And BigBlackSheep. Coming from that point of view( a beginner ). When I first started C++, I never quite understood the continue statement. As to where I would be with my iteration etc.
@Lynx876 You're completely missing the point. It doesn't make anything faster, it just changes flow control.
Topic archived. No new replies allowed.