How to set WHILE condition in a DO WHILE loop

Posted on 2006-05-25
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

    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

    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

    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)
    LVL 44

    Assisted Solution

    try your loop like this:

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

    Expert Comment

    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

    LVL 3

    Expert Comment


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

    LVL 9

    Accepted Solution

    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

    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

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now