PDA

View Full Version : How to Force a Premature For-Loop Iteration



Cyberdude
07-11-2006, 01:53 PM
When writing For-loops, there are times when I want to branch to the loop-terminating ?Next? statement, thereby bypassing a lot of code within the loop. A simple example might look like this:
For N=1 to 50
Cnt = Cnt + 1
If Cnt = 5 Then Goto NextN
? . . . (lots more code here)
NextN:
Next N
In a short loop, there?s ways of writing the statements to bypass certain parts of the code, but this gets more difficult as the loop becomes longer. As a last resort, I usually use a Goto in the manner shown above. Is there a statement that might look like ?Iterate N? that can be used for this purpose? :cleverman

Bob Phillips
07-11-2006, 02:33 PM
Just use Exit For



For N = 1 To 50
Cnt = Cnt + 1
If Cnt = 5 Then Exit For
' . . . (lots more code here)
Next N

mdmackillop
07-11-2006, 02:33 PM
You could move "lots more code" into separate routines


For n = 1 To 50
Cnt = Cnt + 1
If Cnt <> 5 Then DoStuff n
Next n

geekgirlau
07-11-2006, 04:17 PM
Know what you mean, Cyberdude. I try to avoid the use of GoTo whenever possible, but there isn't a built-in method that I know of to branch to the next cycle of the loop. You want to continue the loop, not exit the loop altogether.

johnske
07-11-2006, 07:20 PM
Use a case statement within the loop... e.g.

Option Explicit

Sub DoStuff()
'
Dim N As Long, Cnt As Long
'
'some code here
'
For N = 1 To 50
Cnt = Cnt + 1

Select Case Cnt
Case 1 To 4, 20 To 50
'some stuff here
Case 5
'some other stuff here
Case Else
Exit For
End Select

Next N
'
End Sub

geekgirlau
07-11-2006, 07:54 PM
There are ways around it, but it would be nice to have a single option that says "go to the next loop"

Cyberdude
07-11-2006, 08:17 PM
geekgirlau (you MUST tell me what that means!) you win the prize for understanding what I'd like. :friends:

"Exit For" does just that. I don't want to leave the loop . . . I just want it to do the next iteration without having to execute all the code that follows.

Breaking it up into "submacros" is just too clumsy. I know I can do things like that, but a single statement ("Iterate N"??) would be simple much like the "Exit For" statement.

In any case, I think you guys answered my question. No, there isn't a statement like I want. (sigh)
Thanx gang for giving it some thought.

johnske
07-11-2006, 08:24 PM
If you don't want to Exit the loop and you just want to do nothing but increase your loop index by 1 for certain values of Cnt, simply delete the Exit For in the case statement and that's exactly what'll happen. :)

Bob Phillips
07-12-2006, 02:50 AM
I clearly mis-understood your intent in my first reply, so now that I understand, this is how I would do it



For n = 1 To 50
Cnt = Cnt + 1
If Cnt <> 5 Then
'your stuff
'more of it
'and some more
End If
End Sub


similar to Malcolm, but avoiding sub-procedures, which I would only do there were functionally appropriate code to break-out, and if it improved the readability.

geekgirlau
07-14-2006, 03:48 AM
[quote=Cyberdude]geekgirlau (you MUST tell me what that means!)/quote]

I'm afraid there's no deep, dark mystery here.

I sit at a computer all day (and sometimes half the night if I win the toss with my long-suffering other half), hence the "geek";
I'm not male (although okay, "girl" might be pushing the boundaries of credibility here given the big 4 oh looming ever closer);
I'm an Aussie (and when I first tried to setup the handle "geekgirl" it was already taken).:curtsey:

Cyberdude
07-14-2006, 01:53 PM
Thank you, geekgirlau. I got the geek girl part (I thought), but the suffix "au" always threw me because I tend to see it as "geekgir lau", so I thought maybe it had Hawaiian overtones. Mystery explained.
Don't sweat the "big 4 oh" ... next month I'll hit the bigger "seven 4".