954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

loop that skips a number

I'm trying to make a loop that that will skip a number but has a certain limit to how high it goes...

For example:

I have a for loop:

for (i = 0; i < count;  i++ )
{
    stuff
}



but lets say I want to skip a number in that loop.

Like if count was 10. The loop will display all things from 0 to 10. What if I want it to skip like when i =5?

Acquire
Light Poster
30 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Use the continue keyword. It skips to the next iteration.

for (i = 0; i < count;  i++ )
{
        if ( i == 5 )
                continue;

        stuff
}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Can you look at my other thread on here, the one about help on structors.

This problem pertains to that. I tried doing an if statement to just print "ok" when it comes accross the invalid number. The invalid variable must not have a value or something, cause my if statment is

if(i != invalid)
printf("Ok");

but it always prints prints ok, even though the way I have my data file set up is that the invalid data is on line three, it even prints it in the validate function, but nothing happens later in the other function.

Acquire
Light Poster
30 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

I figured it out.

Acquire
Light Poster
30 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

was it the "return invalid" statement being in the wrong place?

JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

Yeah, I had return (invalid) at the end of the function, which meant it would return invalid after the whole loop had done, so it would always return invalid as 5. I moved it to be in the if statement, that way it actually returns the number of the line where the error is.

Acquire
Light Poster
30 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You