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!

Do Until Loop 3

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Is there a way to skip an iteration of a loop in the middle.
I have a test condition in the middle of a loop, if true I want to continue with the next iteration of the loop
Thanks
 
after u set the end condition to true the loop is executed again and takes time...
if u could do an aplication withowt if conditions and goto u are good...
this way it's much faster io think than your version...
It's just an example...
Even myself i dont use this method cuz i hate goto instruction... but what he need it's a wat to do this more simply
If he is a beginner, will lose the logic of the loop, but using goto it's much easy and simple...

anyway, thanks for consideration... ________

George
 
No, it's not the Goto that is the problem. It's the fact that you're exiting the If statement without Ending it.
 
strongm,

I do not ever recall seing a prohibition aginst exititng an "If" (block) statement " ... without ending it ... ". I believe that it is - in fact - the recommended process for exiting some Loops. There are everal examples of this in Ms. Documentation, Here is one - taken from the VB 4 "Programmer's Guide:

Exiting a Control Structure
The Exit statement allows you to exit directly from a For loop, Do Loop, Sub procedure or Function Procedure. the syntax for the Exit Statement is simple: Exit For can appear as many times as needed insidea a For loop and Exit Do can appear as many times as needed insidea a Do loop:

For counter = start To end [Step increment]
statementblock
[Exit For]
statementblock
Next [counter[, counter] [,...]

Do [{While | Until} condition]
statementblock
Exit Do
statementblock
Loop

[Exit For and Exit Do are useful because sometimes it's appropiate to quit a loop immediately, without performing any further iterations or statements within the loop. For example, in the previous example that printed thn fontscommon to both the Screen and the Printer, the code continues to compare Printer fonts aginst a given Scree font even when a match has already been found with an earlier Printer font. A more efficient version would exit the loop as soon as a match is found:

Private Sun Form_Click()
Dim SFont, PFont
[tab]For Each SFont in Screen.Fonts()
[tab][tab]For Each PFont in Printer.Fonts()
[tab][tab][tab]If SFont = PFont Then
[tab][tab][tab][tab]Print SFont
[tab][tab][tab][tab]Exit For
[tab][tab][tab]End If
[tab][tab]Next PFont
[tab]Next SFont
End Sub

This procedure - given as an example of the 'proper' use of the Exit [For | Do] statement violates the thread arguments aginst the use of several variations of the single in / single out theme.

I am completely unsure of where this new variation on the 'rule' comes from. I AM completly sure it is entirely mis-guided. Even in those 'days of yore' (Yourdon et. al.), there was a recognition [/]AND ACCEPTANCE
of the simple reality that 'the rules' were NOT universal. Including or convolouting code to just satisfy a ideal of form/format was not then (and in MY OPINION) should not NOW be a reason to avoid perfectly good instructions.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Firstly I'm sort of amused that you use a Microsoft example as proof of good coding practice. Secondly, Exit is a slightly different case.

Having said that, I agree in general (as I indicated earlier in this thread) with your views on this subject.
 
The single in single out idea as I know it only applies to encapsulated procedures like functions and subs, not simple routines like loops and conditional blocks (and although I adhere to this and require it of my programmers - simply from a standpoint of code maintainability - I will agree that it is an arguable question of style). As far as exiting an If block 'prematurely', I agree with MichaelRed, I don't know of any better way to do it ...emphasis on better. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top