Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DO WHILE break

Status
Not open for further replies.

dcomartin

Programmer
May 12, 2003
27
CA
Is there anyway to break out of a DO WHILE loop without having to make the while condition false? Their isn't any type of BREAK statement or any statement to break out of a loop? So for example, if you did

DO WHILE .T.
* You would be in an infinite loop.
ENDDO

How would you break out of the loop in the above example?
 
I found EXIT, which breaks out of the current loop. But unfortunatly, you cannot specify how many loops you want to break out of, incase you have nested DO WHILE's. I am guessing this is the reason why all the code im looking never uses EXIT, but rather changes the condition so that its false. Example:

STORE .T. TO goloop
DO WHILE goloop
* your code in here
STORE .F. TO goloop && do this when you want to exit
ENDDO

Is this correct?
 
EXIT is correct. It will transfer program execution to the command immediately after the ENDDO of the current loop. Although I can't recall nesting DO WHILEs in my own work, I would expect that EXIT will break out of the innermost DO WHILE loop being executed, and the program would then be executing commands within the next higher level DO loop. Then you would need to evaluate your loop exit condition for that next level loop to determine whether to exit it, and so on.
 
Yeah... makes sense. I just wish programmesr would comment their freaking code. I have 1000's of lines of code with NO comments anywhere of what is going on. This is brutal. I mean its easy to follow, but it would nice to see some reasoning behind some of the stuff that is written... just my grip.
 
Sorry I type that last one wrong. What I should of said is.... I took over position where there are thousand of lines of code and none of them have any comments. Im digging though all of it trying to make sense of it. The goal is to first understand the whole system, and then be able to make small modifications. At that point I hope to rewrite the system using more modern technologies. I'm used to using Delphi, Java, Python along with MySQL, PostgreSQL, MSSQL 7. At my past jobs, I have always been using recent languages and databases, so getting used to dBase IV is interesting to say the least.
 
A suggestion....

It is good programming practice to supply only one entry and one exit method in any looping function. By supplying exit, break, etc statements and the like, it makes it difficult, and sometimes nearly impossible, to debug a loop with multiple entry/exit possibilities. If you want to terminate a loop (do while...enddo) create a method to change the do while condition to false. This can be any number of things, or a combination of them, even key strokes (pressing the escape key).

By the way, it's easier if you assign a variable to .T. like the following:

true = .T.

do while true
...
...

if (some condition)
true = .F.
endif

enddo


By changing the value of true to .F. the loop will be terminated and program execution can continue elsewhere.

There's always a better way...
 
And then after changing the DO WHILE condition, you can use LOOP to go back to force an immediate retest.

By the way, there are 3 different looping structures, each optimized for a specific need. All allow the use of LOOP (go back to beginning) or EXIT (get out).

DO WHILE / ENDDO -- continues while condition matches
SCAN / ENDSCAN -- steps through a table for matching condition
FOR / ENDFOR (or FOR / NEXT) -- increments in numeric steps

So if any combination of these are nested, you have to be sure which structural loop the LOOP or EXIT affects.
 
if you place the if (some condition) true = .F. endif
at the end of the "do while" loop (just before the "enddo") then you will never have to "loop" back to do a retest. It will happen automatically. But be sure that you take care of writing any data to the file before you leave the do while loop. Usually you'll use a nested "if" statement to handle this and to interact with the user - "Write data to table before exiting? Y/N" or something along those lines. Otherwise, you'll lose any data that was entered into field areas.

There's always a better way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top