PDA

View Full Version : Sleeper: Cleaner Code



MWE
08-31-2005, 10:44 AM
Is there a way to branch to the end of a For loop without extra statements? In FORTRAN, every Do Loop ended with a statement number so it was easy to branch to the bottom of the loop. For example


Do 10 I = 1 to 5

if something go to 10

10 Continue

In VBA, you can accomplish the same thing with, say,


For I = 1 To 5
If something Then goto NextI
NextI:
Next I

Yes, I know that with the proper use of If statments, this kind of branching is not necessary. But humour me ... Is there a way to branch directly to the end of the For loop?

BlueCactus
08-31-2005, 10:54 AM
Exit For

Cyberdude
08-31-2005, 11:47 AM
Curious that you should ask about this because I have oft wondered the same thing. If I understood your question, you are looking for something that (in effect) says "do the next iteration". I've always used (reluctantly) the "GoTo NextI:" approach, but this seems to be a feature that's been omitted from the language. In many cases you can use an "If...Then" modification to the logic to accomplish the desired result, but sometimes that gets REALLY awkward and hard to read.

BlueCactus
08-31-2005, 11:58 AM
Oops! Sorry... misread your post! :doh:

mvidas
08-31-2005, 12:11 PM
Unfortunately, there is nothing like LoopNext or NextFor or anything like that. I've used things like this before:


For i = 1 To 100
Do
'code i want to repeat with current value of i until something happens
If WhatIWantToHappenHappens Then
Exit Do
End If
Loop
Next 'i

but generally if you have good if blocks and the occasional boolean variable to test, you shouldnt need it. I never use gotos anymore though, doesn't look right and seems like i just threw the code together :(
Matt

MWE
08-31-2005, 12:26 PM
Exit For
thanks, but Exit For will exit the loop. I do not want to do that. Rather I want to drop to the bottom, increment the index, ...

and I should have read all the replies ...

MWE
08-31-2005, 12:30 PM
Curious that you should ask about this because I have oft wondered the same thing. If I understood your question, you are looking for something that (in effect) says "do the next iteration". I've always used (reluctantly) the "GoTo NextI:" approach, but this seems to be a feature that's been omitted from the language. In many cases you can use an "If...Then" modification to the logic to accomplish the desired result, but sometimes that gets REALLY awkward and hard to read.
I agree. It can be really awkward and defeats the original purpose of "not using go to", i.e., the code becomes very hard to read.