Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Skip an iteration in For...Next loop

Status
Not open for further replies.

jfrizelle

Programmer
Jan 15, 2004
26
0
0
IE
Hi

How can I skip to the next iteration of a for next loop? Eg. I'm on iteration 5 but certain criteria mean I don't want to execute the remaining code, so I want to skip on to iteration 6. I thought the keyword was 'Continue' but apparently not. I can only find 'Exit For' which is not what I want to do.

Many thanks,
Jennie.
 
Hi!

Might perhaps be more elegant solutions, but this works...

[tt]for i = 0 to 1000
if ctriterion <> somevalue then
' perform the code
end if
next i[/tt]

Roy-Vidar
 
I would create a boolean variable called something like "Skip" set it to true when you want to skip the next iteration and then use an if statement within your loop:

if Skip = False then
'YOUR CODE
if <your argument for skiping the next iteration> then Skip = True
else
'reset the Skip variable
Skip = False
endif
 
Thanks a lot - I did the skip option. Am I correct in thinking there used to be a Continue option - or am I thinking in a different language? From memory, Continue would go on to the next iteration, and Break would exit the loop - please tell me this thinking has some basis in reality, and I'm not losing the plot!!
 
No, you're not losing it. The Continue statement in "C" in a for next loop skips to the next iteration. You will also find that in many of the C derivative languages as well, such as JAVA and PHP.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top