Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to set WHILE condition in a DO WHILE loop

Posted on 2006-05-25
Medium Priority
Last Modified: 2012-05-05

I am quite new to programming so my question might seem a little silly. Please bear with me.    I have a DO While loop. My Problem is that I want to set the WHILE condition I want to say that perform the statements in the Do loop WHILE the statement m=m+1 is executed. The m=m+1 statement is somewhere else in the code. How can I set my WHile condition? Below is my DO While loop:

Private Function RepeatLoop()
     If m <= (UBound(SubDir) - 1) Then
            ReadContents SubDir(m), m
           Exit Function
      End If
  Loop While  (m= m+1   Is Not Executed) --- HOW TO SET THIS CONDITION
End Function

I have m as a global variable whose value is being used everywhere in code.

Thanks for any help.

Question by:pmud
LVL 23

Expert Comment

ID: 16765538
Is this a multithreaded application?  I kind of doubt it is, in which case you have only a single thread.  When the single thread executes the Do loop it will be stuck in there and will never execute the m=m+1.  

I would like to know what you are trying to do...

LVL 24

Assisted Solution

SunBow earned 900 total points
ID: 16765632
1>  WHILE the statement m=m+1 is executed
2>  While  (m= m+1   Is Not Executed)

er, make up your mind is a good step.

It helps me to run a cheat. By knowing what the rest of the code is (such that m is not used anywhere else than than one line of incrementation)

before entering loop, you can set another variable to 'remember' what m was for you:


Then you can execute the loop while n=m
This satisfies condition #2, for as soon as m changes, the difference can be detected.

For the other condition, #1, you'd need place the 'remembrance' of n=m inside the loop, and after the incrementation may or may not occur, if that is a random event or whatever.

As fyi, the question is unusually phrased.

Usually, for an 'Until' or a 'while', we do that on the same line

ex: Do m=m+1 While m<=10
ex: Do m=m+1 Until m=10

Something more like that, the syntax depends on language,

> m as a global variable

A good reason to create a new local variable that is set to the global one.
LVL 24

Assisted Solution

SunBow earned 900 total points
ID: 16765744
global variables should really have longer names, if only generic global_m to help them stand out

> The m=m+1 statement is somewhere else in the code.

Wherever that is, you have to set the backup prior to that. I thought you meant anywhere in the loop. Let 'n' be the backup.


...                     ;lots of stuff, anywhere
m=m+1             ; note, this is probably conditional, may be executed, maybe not
...                     ;lots of stuff
while n=m
  - or -
while n<>m

(depending on #1 or #2 need)

--------- ---------------

Maybe like basicinstinct indicates, you need to get a better definition of what to do, or maybe you just want alternative, such as a:

Do        m=m+1
.....                                                           ;stuff
Loop     while     m <= (UBound(SubDir) - 1)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 44

Assisted Solution

Arthur_Wood earned 300 total points
ID: 16766073
try your loop like this:

m = 1
Do While  m <= (UBound(SubDir) - 1)
    ReadContents SubDir(m), m
    m = m + 1

Expert Comment

ID: 16769299
i don't know what u r doing.. :)

m will never be = to m+1..  :O

try to redefine what you want with some logic


Expert Comment

ID: 16769358

i think you should explain in english what u want to do ,so we can help u


Accepted Solution

lojk earned 300 total points
ID: 16769470
It is sometimes simpler to do

Private Function RepeatLoop()
     If m <= (UBound(SubDir) - 1) Then
            ReadContents SubDir(m), m
           Exit Do
      End If
End Function

Author Comment

ID: 16777188
Hi All,

Thanks for your responses. I have already set the condition for my Do WHile loop based on another variable n which increments itself everytime a the user clicks a button and some processing occurs as mentioned by SunBow . But I read about Application.DoEvents just now and I think I was having trouble eariler with my Do while loop,since all my program was within a do while loop, but I didnt want the loop to process again until an event occurred (done by user) . I think Application.Doevents does that if i am right. Thanks lojk for letting me know this important aspect.

Thanks a lot to all of you for the answers . It was very helpful.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question