cond

AutoCad AutoLISP Functions

 
cond
 
 
 

Serves as the primary conditional function for AutoLISP

(cond [(test result ...) ...]) 

The cond function accepts any number of lists as arguments. It evaluates the first item in each list (in the order supplied) until one of these items returns a value other than nil. It then evaluates those expressions that follow the test that succeeded.

Return Values

The value of the last expression in the sublist. If there is only one expression in the sublist (that is, if result is missing), the value of the test expression is returned. If no arguments are supplied, cond returns nil.

Examples

The following example uses cond to perform an absolute value calculation:

(cond 
   ((minusp a) (- a)) 
   (t a)
) 

If the variable a is set to the value -10, this returns 10.

As shown, cond can be used as a case type function. It is common to use T as the last (default) test expression. Here's another simple example. Given a user response string in the variable s, this function tests the response and returns 1 if it is Y or y, 0 if it is N or n; otherwise nil.

(cond
   ((= s "Y") 1) 
   ((= s "y") 1) 
   ((= s "N") 0) 
   ((= s "n") 0) 
   (t nil)
)