By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

Help with a loop, print 1-20 to the screen but skips 15, 16, and 17

P: 14
I need help with writing a loop that prints 1-20 to the screen but skips 15, 16, and 17.

I know how to write a for loop that prints any number range, such as 5-200 1-20 etc. But I don't know how to make it skip specific numbers. Please help.

Thanks.
Oct 26 '06 #1
Share this Question
Share on Google+
6 Replies


arne
Expert 100+
P: 315
I need help with writing a loop that prints 1-20 to the screen but skips 15, 16, and 17.

I know how to write a for loop that prints any number range, such as 5-200 1-20 etc. But I don't know how to make it skip specific numbers. Please help.

Thanks.
Try an 'if'-statement and the 'continue' statement ... :)
Oct 26 '06 #2

P: 14
Try an 'if'-statement and the 'continue' statement ... :)
Hmm, i'll try to figure it out.
Oct 26 '06 #3

P: 14
This is supposed to be a loop. I didnt think an 'if' statement was a loop, for printing 1-20 I used a for loop.
Oct 26 '06 #4

P: 7
This is supposed to be a loop. I didnt think an 'if' statement was a loop, for printing 1-20 I used a for loop.
#include <iostream>
using namespace std;

void printNum()//print 1-20 to the screen but skips 15, 16, and 17
{
int x;
for(x=1;x<=20;x++)
{
if( (x ==15) || (x==16) || (x==17))
continue;

cout <<x<<" ";
}

return;
}
int main()
{
printNum();


system("PAUSE");
return 0;
}
Oct 27 '06 #5

P: 14
I got it, it was easier than I thought! This is what I wrote, and it worked.

#include <iostream.h>

int main ()
{
int n;

for (n=1; n<21; n++)
{
if (n==15) continue;
if (n==16) continue;
if (n==17) continue;
cout << n << "\n";
}
return 0;
}
Oct 27 '06 #6

P: 7
Great Stuff.
Oct 27 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.