×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

DO WHILE break

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?

RE: DO WHILE break

(OP)
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?

RE: DO WHILE break

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.  

RE: DO WHILE break

(OP)
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.

RE: DO WHILE break

(OP)
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.

RE: DO WHILE break

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...

RE: DO WHILE break

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.

RE: DO WHILE break

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...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close