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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a command to go back to the top of a loop structure? 3

Status
Not open for further replies.

GZook

Programmer
Apr 14, 2003
42
0
0
US
I have the following loop in my program:
Code:
    lSkipEOF = 1
    Do While lSkipEOF = 1
        'Record specific variable initialization
  
        'No Tissue Quantity entered
        If Left(sTissueUser, 1) = "Y" And Len(Trim(sTissueQty)) = 0 Then
            lRetVal = MsgBox("Tissue User with no Tissue Quantity entered. Unable to Update Priority Field.", vbOKOnly)
            lSkipEOF = GMW_DB_Skip(lArea2, 1)
            ' How can I go back to top of this loop?  
            'Exit Do leaves the loop
        End If

        'Further Processing

        'Move to next record in CONTACT2
        lSkipEOF = GMW_DB_Skip(lArea2, 1)
    Loop

From within the If structure, how can I make execution go immediately to the top of the Do While structure?
 
Maybe I'm making this too simple, but since you're using the IF to determine that you no longer need processing that particular set of data, why don't you use the ELSE to continue the processsing. ie...


lSkipEOF = 1
Do While lSkipEOF = 1
'Record specific variable initialization

'No Tissue Quantity entered
If Left(sTissueUser, 1) = "Y" And Len(Trim(sTissueQty)) = 0 Then
lRetVal = MsgBox("Tissue User with no Tissue Quantity entered. Unable to Update Priority Field.", vbOKOnly)
lSkipEOF = GMW_DB_Skip(lArea2, 1)
' How can I go back to top of this loop?
'Exit Do leaves the loop
Else

'Further Processing

'Move to next record in CONTACT2
lSkipEOF = GMW_DB_Skip(lArea2, 1)
Endif
Loop


 
StormBringerX,

I've thought of that. I was just wondering if there was such a command so that I wouldn't have a program with an indent level for each nested validation if structure.

 
You could fall back to the GoTo statement

Private Sub Command1_Click()
1: GoTo 6
2: MsgBox "Hello"
3: GoTo 5
4: MsgBox "Never Get Here"
5: Exit Sub
6: GoTo 2
End Sub

It may be ugly but it will do the job.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Foada,

Yes, yes! I get it now!
Code:
Goto 6
as in Goto program line 6, etc! I can live with it if I have to change the number manually when the program is modified. Yes! And since all the validation If structures will all go to that same Do While Line, it will work nicely. That's what I'm looking for! Thanks! Now I just have to determine what line number the Do While line is. Thanks. You just made my day!
 
DrJavaJoe,

I agree, hence the ugly comment. [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
If you're bound and determined to use the GOTO instead of If/Then/Else, instead of line numbering, I would goto a label.

Do While
lalalalal
If Something then Goto ContinueProcessing

lalalalal
lallalala

ContinueProcessing:
Loop
 
StormBringerX,

Labels don't need changing! Even Better! Thank you!
 
Ok, as you can probably tell, I'm quite new to Visual Basic. My background is 17 years of FoxPro/dBase experience, starting way back in '85 with dBase II when there were only two work areas known as Primary and Secondary, meaning you could only have two tables open at one time.

So this is almost like starting over again, though there are a lot of similarities between VB and VFP.
 
The general consensus is that Goto statements tend to make code difficult to follow. Goto statements are usually used in error handling such as On Error Goto ErrorHandler. I suppose you could use with very complex nested control structures but I wouldn't and for what you are trying to do I would do as StormbringerX had originally suggested.
 
DrJavaJoe,

You've got a good point, and I agree with you. It does get confusing when one GoTo leads to another and another, etc.

However in this circumstance, the use of GoTo isn't confusing at all. This code uses the GMW_DB_SKIP() function in the GoldMine GMW6S32.DLL library which returns 1 if successful.

Code:
.
.
.
lSkipNo = 1
Do While lSkipNo = 1
    .
    .
    .
    'Coding
    .
    .
    .
    ' Only three conditions to check
    If bad condition1 Then
       Message1
       GoTo EndLoop
    End If 
    If bad condition2 Then
       Message2
       GoTo EndLoop
    End If 
    If bad condition3 Then
       Message3
       GoTo EndLoop
    End If
    .
    .
    .
    ' Good record processing
    .
    .
    .
EndLoop:
    lSkipNo = GMW_DB_SKIP(lArea2)
Loop
.
.
.

This is the only kind of instance in which I would use this. I find it to be quite readable as all three GoTos point to the same place with a descriptive label. In fact, this is quite similar to what I would've done in VFP which follows: The && and * are comments.

Code:
SCAN && automatically exits upon EOF
    IF badcondition1
        Message1
        LOOP && Skip and go back to beginning of loop
    ENDIF
    IF badcondition2
        Message2
        LOOP && Skip and go back to beginning of loop 
    ENDIF
    IF badcondition3
        Message3
        LOOP && Skip and go back to beginning of loop
    ENDIF
    .
    .
    .
    * Good Record Processing
    .
    .
    .
ENDSCAN && does an automatic SKIP
 
This is how I would do it.

lSkipNo = 1
Do While lSkipNo = 1
.
.
.
'Coding
.
.
.
' Only three conditions to check
If bad condition1 Then
Message1
lSkipNo = GMW_DB_SKIP(lArea2)
elseIf bad condition2 Then
Message2
lSkipNo = GMW_DB_SKIP(lArea2)
Elseif bad condition3 Then
Message3
lSkipNo = GMW_DB_SKIP(lArea2)
else
.
.
.
' Good record processing
.
.
.
End If

Loop
 
DrJavaJoe,

Sorry it took me so long to respond. Just got back to my computer.

This doesn't look as bad as I thought it would. I forgot about the ElseIf on one line (One of those things VFP 6 doesn't have.) So, in reality, it's only one more indentation level for the final Else. In fact, this kinda looks like a VFP DO CASE structure. Hmmm. Guess you can teach an ol dog new tricks after all! [2thumbsup]
 
With that said here's another approach.

lSkipNo = 1
Do While lSkipNo = 1
.
.
'Coding
.
.
' Only three conditions to check
Select Case BadCondition
Case condition1
Message1
lSkipNo = GMW_DB_SKIP(lArea2)
Case condition2
Message2
lSkipNo = GMW_DB_SKIP(lArea2)
Case condition3 Then
Message3
lSkipNo = GMW_DB_SKIP(lArea2)
Case else
.
' Good record processing
.
End select
Loop
 
Is this kind of "holy war"?
Like, "GOTO's ARE BAD BECAUSE THEY ARE BAD"?

In C language one have "continue" statement
that (quote from help file)

Causes control to pass to the end of the innermost enclosing while, do, or
for statement, at which point the loop continuation condition is
re-evaluated.

From GZook post:
there is operator "LOOP" in FoxPro that serves the same.

So if you need this (and VB have no such thing) - why not simulate it with GOTO to the end of the loop?

You have the job done, after all.
 
tsh73
They are bad because they quickly make the code hard to read and you really don't need to use them, especially in the example that we are talking about.
 
DrJavaJoe,
peace ;)
"especially in the example that we are talking about."
You are completely right here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top