DO WHILE break
DO WHILE break
(OP)
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?
DO WHILE .T.
* You would be in an infinite loop.
ENDDO
How would you break out of the loop in the above example?
RE: DO WHILE break
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?
RE: DO WHILE break
RE: DO WHILE break
RE: DO WHILE break
RE: DO WHILE break
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...
RE: DO WHILE break
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.
RE: DO WHILE break
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...