Lesson25 – A More Versatile Program Branch- the (cond) Function

Many decision branches are multi-forked—that is, there are several possible outcomes, each with a unique result. Rather than nest a series of (if) statements, a single (cond) function can handle a multi-fork branch extremely well.

Explanation: the (cond) function






(T nil)

) ;end cond

  • Each test expression is evaluated sequentially until one is found that is non-nil. It then evaluates those expressions that follow the test that succeeded, returning the value of the last of these expressions.
  • Any number of test expressions is possible.
  • It is common to use T as the final test expression as an error-checking mechanism in case all other tests fail. For example:

( T (prompt “\nNo conditions were met.”))


(defun c:object ()
      (initget 1 “Circle Square Rectang”)
     (setq CMD (getkword “\nEnter object type Circle/Square/Rectangle: “))
     (setq PT (getpoint “\nEnter a point: “))
         ((= CMD “Circle”) (command “circle” PT 0.5))
         ((= CMD “Square”) (command “pline” PT “@1<0” “@1<90” “@1<180” “c”))
         ((= CMD “Rectang”) (command “pline” PT “@2<0” “@1<90” “@2<180” “c”))

  • A copy of this example is on your class disk as OBJECT.LSP


Create an AutoLISP routine that will let the user select from a list of blocks and insert it. Test it in the file OFFICE.dwg. Estimated time for completion: 10 minutes.

Steps to solution

1. Ask user to select from a list of blocks.
2. Test which block name was chosen and insert the block.


(defun c:furn ()
(initget 1 “Chair Desk PC Plant”)
(setq FURN (getkword “\n Enter furniture type to insert Chair/Desk/PC/Plant: “))
((= FURN “Chair”) (command “insert” “Chair” “s” “1” pause pause))
((= FURN “Desk”) (command “insert” “Desk” “s” “1” pause pause))
((= FURN “PC”) (command “insert” “PC” “s” “1” pause pause))
((= FURN “Plant”) (command “insert” “Plant” “s” pause pause))

  • A complete solution to this exercise is on your class disk as FURN-A.LSP.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.

Show Buttons
Hide Buttons

It's FREE!

Sign up and receive an email every time a new tutorial is published