returning back to loop check condition without completing the loop

  • Thread starter Thread starter ashish128
  • Start date Start date
A

ashish128

Hi All,

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.

Regards

Ashish
 
Do While some_condition = TRUE

'-----Some Code----

If this_condition = TRUE then
'do nothing
else
'some code here
End If

'no code here!!!!
loop
 
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


Rick
 
Do While some_condition = TRUE

      '-----Some Code----

       If this_condition = TRUE then
           'do nothing
       else
           'some code here
       End If

       'no code here!!!!
loop














--

Dave Peterson- Hide quoted text -

- Show quoted text -

Hi Dave,

I know this will work but I cannot do this. I just want to stop
execution and return it to loop checking condition.
Besides this, I need to apply this at several places in my loop.

Thanks for your help
 
Do While some_condition = TRUE

      '-----Some Code----

       If this_condition = TRUE then
           'do nothing
       else
           'some code here
       End If

       'no code here!!!!
loop














--

Dave Peterson- Hide quoted text -

- Show quoted text -

Dear Dave,

My question is to break the process flow in middle of a loop and
restart it from the loop condition checking.

Regards
 
You didn't like Rick's reply?
Dear Dave,

My question is to break the process flow in middle of a loop and
restart it from the loop condition checking.

Regards
 
You didn't like Rick's reply?









--

Dave Peterson- Hide quoted text -

- Show quoted text -

Dear Dave,

I must say that there is some problem with the GUI.

My post says 6 messages but I cannot view more than 4 (two mine and
two of yours)

Therefore I dont even know if Rick has suggested some solution.

Please convey my apologies to Rick (If you can) as I cannot see a
single post of his.

Thank You.

Regards
 
Hoping that you will be able to see this response from me, here is what I
posted originally...

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

Please indicate if you can see this message.

If Ashish doesn't respond in a reasonable time period, will someone be kind
enough to copy and post it back to Ashish for me.

Rick



You didn't like Rick's reply?









--

Dave Peterson- Hide quoted text -

- Show quoted text -

Dear Dave,

I must say that there is some problem with the GUI.

My post says 6 messages but I cannot view more than 4 (two mine and
two of yours)

Therefore I dont even know if Rick has suggested some solution.

Please convey my apologies to Rick (If you can) as I cannot see a
single post of his.

Thank You.

Regards
 
Hoping that you will be able to see this response from me, here is what I
posted originally...

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

Please indicate if you can see this message.

If Ashish doesn't respond in a reasonable time period, will someone be kind
enough to copy and post it back to Ashish for me.

Rick






Dear Dave,

I must say that there is some problem with the GUI.

My post says 6 messages but I cannot view more than 4 (two mine and
two of yours)

Therefore I dont even know if Rick has suggested some solution.

Please convey my apologies to Rick (If you can) as I cannot see a
single post of his.

Thank You.

Regards- Hide quoted text -

- Show quoted text -

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.

Regards
 
I apologise if this question has been posted before but to be true,
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
 
Why not use the "nested if" if you don't like GoTo's?












--

Dave Peterson- Hide quoted text -

- Show quoted text -

Dear Dave,

To be true, I didn't use "nested if" because I was preferring a clean
solution as otherwise it would cause me to have (many if and) deep
look into my code for any alteration if I break the loop at many
points.

Second truth, It didnt came to my mind :)

I am applying Rick's suggestion to get the code work as intended.

Thanks for your guidance (If it had not been you pointing to Rick's
Post I would have never come to know about it)

Regards
 
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
 
If the code starts looking too bad, you could always call subroutines from
either branch of your if/then/else statement. And do all the detail work there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top