Re: returning back to loop check condition without completing the loop

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mar 31, 8:00 pm, "Rick Rothstein \(MVP - VB\)"
<rick.newsNO.S...@xxxxxxxxxxxxxxxxxx> wrote:
 I apologise if this question has been posted before but to be true,
 I could not find it. I have a very little question. Please see below

Do While some_condition = TRUE
 '-----Some Code----
 If this_condition = TRUE then
 ' Here I would like to send the execution back to "Do
 While some_condition = TRUE"
 ' and bypass all succeeding lines of code
 End If
 '-----Some Code----
 loop

Kindly let me know if this is supported / possible in VBA.

You could use a Label Statement and a GoTo statement like this...

Do While some_condition = TRUE
'-----Some Code----
If this_condition = TRUE then GoTo Continue
'-----Some Code----
Continue:
Loop

Dear Rick,

Yes, now I can see your post (But still my post says 8 Messages
and I am ble to see only 6).

Thanks for the suggestion. Being a programmer (in C Language)
I was taught not to use "Goto" as a primary rule.

But if you are suggesting this then there must be no ther way.

Thank you again for showing the way.

It is not that GoTo should never be used; it's just that it should not be
misused. In order to duplicate the "Continue" statement that other languages
have, the construction I used is pretty much it. Now, it should only be used
when you are going to have multiple points within your loop where you will
want to skip to the next loop iteration from. For the simple construction
you gave, I would just use an If-Then housing to perform the "skip". Using
the structure of your originally posted code, something like this could be
used to skip to the next loop iteration...

Do While some_condition = TRUE
    '-----Some Code----
    If this_condition = FALSE Then
        '-----Some Code----
    End If
Loop

But, as I said, this is a simple construction. As you add more and more
points to "Continue" from, the nesting of the various "Continue points" can
get quite messy quite quickly; and the inverted logical tests in the various
If-Then statements to implement it can get harder and harder to follow if
you ever have to come back to the loop to modify it at some future date. The
GoTo solution I posted earlier simplifies the overall construction while
allowing you to keep your logical If-Then tests in the way you would
naturally think of them.

Rick- Hide quoted text -

- Show quoted text -

Dear Rick,

I agree to the points.

I will be using "Goto" only where no other logical flow exists.

Thanks again for your great help

Regards
.



Relevant Pages

  • Re: returning back to loop check condition without completing the loop
    ... > You could use a Label Statement and a GoTo statement like this... ... In order to duplicate the "Continue" statement that other languages have, the construction I used is pretty much it. ... As you add more and more points to "Continue" from, the nesting of the various "Continue points" can get quite messy quite quickly; and the inverted logical tests in the various If-Then statements to implement it can get harder and harder to follow if you ever have to come back to the loop to modify it at some future date. ...
    (microsoft.public.excel.programming)
  • Re: Iteration in lisp
    ... state machine is just a tagbody and goto. ... But if you need to change the state-transition-matrix in the middle ... loop and either table-driven or separate-functions for defining the ... (tagbody state1 (trace-state1 aux) ...
    (comp.lang.lisp)
  • Re: COBOL aint quite dead - yet !
    ... of control there is nothing wrong with goto. ... The loop is entirely abstracted away. ... Then we have well-formed loops with a single invariant and a looping ...
    (comp.lang.cobol)
  • Re: QBasic to BBC BASIC translator updated
    ... constructs to if then plus goto statements. ... Although a compiler doesn't need to use a stack explicitly to ... to jump out of a loop is the issue of 'static scope'. ... of any other interpreted BASICs that work that way, ...
    (comp.lang.basic.misc)
  • Re: acceptable use of goto?
    ... loop, forward within the loop at various points, then forward out of ... Then you give every statement a label. ... No goto statements. ... Then I build a framework (of if or switch statements) to eliminate the ...
    (comp.lang.c)