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

x
?
Solved

How to set WHILE condition in a DO WHILE loop

Posted on 2006-05-25
8
Medium Priority
?
424 Views
Last Modified: 2012-05-05
Hi,

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()
 
 Do
     If m <= (UBound(SubDir) - 1) Then
            ReadContents SubDir(m), m
       Else
           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.

0
Comment
Question by:pmud
8 Comments
 
LVL 23

Expert Comment

by:basicinstinct
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...

0
 
LVL 24

Assisted Solution

by:SunBow
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:

n=m

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.
0
 
LVL 24

Assisted Solution

by:SunBow
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.

Sequence:

n=m
...                     ;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)
0
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

by:Arthur_Wood
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
Loop
0
 
LVL 5

Expert Comment

by:t_itanium
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

cheers
0
 
LVL 3

Expert Comment

by:zawedikian
ID: 16769358
hi

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

zeina
0
 
LVL 9

Accepted Solution

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

Private Function RepeatLoop()
  Do
     If m <= (UBound(SubDir) - 1) Then
            ReadContents SubDir(m), m
      Else
           Exit Do
      End If
  application.doevents
  Loop
End Function
0
 

Author Comment

by:pmud
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.
0

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