Function Definitions

 5  -  Conditionals

if ..... Applies a test.  If the test passes (returns T), one expression is executed.  If the test fails (returns nil) another (optional) expression is executed.

(if (> height limit) "too high")  returns "too high" if the value in the variable height is greater than the value in the variable limit. Otherwise returns nil.

(if (minusp w) (/ w 2.0) (* w 1.5))  If the value in w is. negative, the second argument is executed.  If it is positive, the third argument is executed.

Function definitions for the tests are given in "6 - Arithmetic tests" and "7 - Logic & category tests."


cond ..... Performs a series of tests until one of those tests passes (returns non-nil) whereupon the response that follows that test in the same list is executed.

Following the cond function is a series of lists. Within each list is one test expression and one or more response expressions. After one of the test expressions returns T and its response(s) are executed, no further tests are performed -- the cond function is exited.

(cond
   ((> j 100) (setq  j 100))
   ((< j -100) (setq  j -100)))

Examine the first list (the entire line) after the cond function.  The test is the first element in this list, which is (> j 100).  If this test passes, j is set to 100.  If this test fails, then the AutoLISP interpreter moves on to the next list and runs its test, which is (< j -100).  If this test passes, j is set to -100.  If neither test passes, then the cond function closes without any action.

(cond
   ((>= pct 90.0) (setq grade "A"))
   ((>= pct 80.0) (setq grade "B"))
   ((>= pet 70.0) (setq grade "C"))
   ((>= pct 60.0) (setq grade "D"))
   (1 (setq grade "F")))

The above expression assigns a letter grade to the variable grade based on the percentage in variable pct.

The  1  in the last list is actually the test.  Since  1  always evaluates to  1 (non-nil), the test in the last list always passes, just in case all the other tests fail.  This last list is a "catch-all" list.  By the way, many AutoLISP books suggest the use of the pre-set variable  T  for this purpose instead of  1.  However,  T can be accidentally reset, whereas  1  cannot.  So using  1  is the wiser choice.

Function definitions for the tests are given in "6 - Arithmetic tests" and "7 - Logic & category tests."


HOME


Copyright © 1988, 1998 Ronald W. Leigh