while

Discussion in 'AutoCAD' started by Chuck, Jul 24, 2004.

  1. Chuck

    Chuck Guest

    With much reading and practice I have become a bit familiar with writing
    autolisp routines. Things I couln't do a year ago are now becoming quite
    simple.
    One thing, however I'm having trouble with is using "while" for loops. Can
    anyone explain the "while" function and how it works, maybe with a couple of
    examples.
    I'm especially baffled by how it is used to read down a data list, looking
    for info. Any help would be appreciated.
     
    Chuck, Jul 24, 2004
    #1
  2. Chuck

    Paul Turvill Guest

    (while (some condition exists)
    (do this)
    (and do this)
    (and this...)
    );; end while

    "Some condition" can be T or nil, an equality, or whatever. For example:

    (setq counter 10)
    (while (> counter 0)
    (princ (strcat "\n" (atoi counter)))
    (setq counter (1- counter))
    );; while
    (princ)

    As for your "data list" if it's in the form of a LISP list, you can use the
    counter variable to get the "nth" element of the list, as in (setq dat (nth
    datalist counter)) and so on.

    If you're reading data from a file, all you have to do is to verify that
    there's another element available:

    (while (setq dat (read-line infile))
    (do things)
    (do more stuff)
    )

    (read-line ...) returns nil after it passes the last element of the file,
    exiting the loop.
    ___
     
    Paul Turvill, Jul 24, 2004
    #2
  3. Chuck

    Chuck Guest

    Thanks Paul. That was very simple yet concise. It will help a great deal.
    Thanks again.
     
    Chuck, Jul 25, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.