The AutoLisp Advanced Tutorial


Loop Functions :

While    Repeat

Example Programs


While  

          Syntax : (while    expression   dothis)

          expression - Any valid autolisp expression that evaluates to true or non nil.

          dothis - Any valid autolisp expression or expressions

                  Note:   No PROGN  necessary for multiple expressions like the IF statement.

                 (while T (princ 1)) returns 1 and keeps returning 1 forever.  Your locked into the loop.

                 (while nil (princ 1)) Exits the loop.

                ----------------------------------------------------------

                 (setq cntr 1)                        setup a counter  

                 (while (< cntr 4)                    loop until cntr is not less than 4

           (princ cntr)                      print the cntr

           (setq cntr(+ cntr 1))             increment the counter

        )                                                                             returns   1 then 2 then 3 and then exits the loop

                 ----------------------------------------------------------

                 (setq eset(ssget))                                       returns a selection set

                 (setq cntr 1)                                             setup a counter

                 (while (< cntr (sslength eset))       loop through all entities in the selection set  

           (setq en(ssname eset cntr))        get the entity name of the nth item 

           (setq enlist(entget en))           get the DXF group codes of the entity

           (princ (cdr(assoc 0 enlist)))      print the entity type to the command line

           (setq cntr(+ cntr 1))              increment the counter

        )                                                                                close the while loop

 

repeat - This function does exactly what you think it would do.

          Syntax : (repeat integer)

          integer - Any number that is an integer.

                  Note:   No PROGN  necessary for multiple expressions like the IF statement.

 

                 (repeat 20

          (princ "A")

        )                                                                       prints the letter "A" to the command line twenty times

                  ----------------------------------------------------------------------------------------

                 (setq a 1 b 5)                       setup some variables.

                 (repeat 4                         repeat these functions 4 times.

                   (setq a (+ a 1))                                   add 1 to [a] each loop

                   (setq b (+ b 5))                                   add 5 to [b] each loop

                 )                                 close the loop

                 (princ a)                         prints 5 to the command line

       (princ b)                         prints 25 tot he command line

 


Example Program 1:

(defun C:DrawLines()                                            [define program] 

  (setq pt1(getpoint "\n First Point: "))                       [get first point]

  (while (/= nil (setq pt2 (getpoint pt1 "\n Next Point: "))) [get next point]

     (command "line" pt1 pt2 "")                                [draw a line]

     (setq pt1 pt2)                                                           [set pt1 to last point]

  )                                                            [close the loop]

)                                                                                                                                          [close the program]

 

Example Program 2:

(defun C:LetterGuess()                          define a program 

  (setq myAnswer "J")                            set up a variable to hold the answer

    (setq yourGuess nil)                           set up a variable to hold your guess

  (while (/= myAnswer yourGuess)                 while you don't know the answer

    (setq yourGuess(getstring "\n Guess what letter: ")) get your guess

  )                                              close the loop

)                                                close the program

 

Example Program 3:

(defun C:PrintVert()                             define a program 

  (setq str(getstring "\n Enter a string:"))    get a string from the user

  (setq cntr 1)                                  setup a counter

   (repeat (strlen str)                          loop once for each character in string

    (setq myChar(substr str cntr 1))             get nth character in string

    (princ (strcat "\n " myChar))                print a new line then the nth character         

    (setq cntr(+ cntr 1))                        increment the counter         

  )                                              close the loop

)                                                close the program

End of Loop Functions


AutoLisp Advanced Tutorial

AutoLisp Tutorial