Conditionals.
(If) is probably the most important and widely use condition
statement.
Unlike other languages though, you can match only one (if) statement with
a then statement. The syntax is as follows :
(if xyz
(then do this)
(else do this)
)
Let's look at a simple example :
(defun c:testif ()
(setq a (getreal "\nEnter a Number : ")
b (getreal "\nEnter Second Number : ")
);setq
(if (= a b)
(prompt "\nBoth Numbers are equal")
(prompt "\nBoth numbers are not equal")
);if
(princ)
);defun
(princ)
If you need to evaluate more than one then, or else
statement, you must use the (progn) function. Here's another example :
(defun c:testprogn ()
(setq a (getreal "\nEnter a Number : ")
b (getreal "\nEnter Second Number : ")
);setq
(if (= a b)
(progn
(prompt "\nBoth Numbers are equal")
(prompt "\nHere is Another statement")
(prompt "\nAnd Another One")
);progn
(prompt "\nBoth numbers are not equal")
);if
(princ)
);defun
(princ)
You can use as many statements as you like within the (progn) function.
You can also use (if) along with logical operators. They are functions
that determine how two or more items are compared. The available logical
operators are :
AND OR NOT
AND returns true if all arguments are true.
OR returns true if any of the arguments are true.
NOT returns true if it's argument is false and returns false if it's
argument is true. Let's look at some examples :
(defun c:testand ()
(setq a (getreal "\nEnter a Number : "))
(if
(and
(>= a 5.0)
(<= a 10.0)
);and
(prompt "\nNumber is between 5 and 10")
(prompt "\nNumber is less than 5 or greater than 10")
);if
(princ)
);defun
(princ)
(defun c:testor ()
(setq a (getstring "\nAre you Male? Y/N : "))
(if
(or
(= a "y")
(= a "Y")
);or
(prompt "\nHello Sir")
(prompt "\nHello Madam")
);if
(princ)
);defun
(princ)
A Relation Operator is a function that evaluates the relationship between
two or more items. Relationship Operators available are :
< less than
> greater than
<= less than or equal to
>= greater than or equal to
= equal to
/= not equal to
eq are two expressions identical
equal are two expressions equal
Let's look a bit closer at the (eq) and the (equal) functions.
The (eq) function determines whether two expressions are bound to the same
object.
(setq a '(x y z))
(setq b '(x y z))
(setq c b)
(eq a c) would return nil, a and c are not the same list.
(eq c b) would return true, b and c are exactly the same list.
The (equal) function determines whether two expressions evaluate to the
same thing. You can use the optional numeric argument, fuzz, to
specify the maximum amount by which both expressions can differ and still
be considered equal.
(setq a '(x y z))
(setq b '(x y z))
(setq c b)
(setq m 1.123456))
(setq n 1.123457))
(equal a c) would return true.
(equal c b) would return true.
(equal m n) would return nil.
(equal m n 0.000001) would return true.
What about a Multiple (if) function. The (cond) function works very much
like (if), except (cond) can evaluate any number of test conditions.
Once (cond) finds the first condition that is true, it processes the
statements associated with that condition. (It only processes the first
true condition). Here's an example :
(defun c:testcond ()
(setq a
(strcase (getstring "\nSize of Bolt (M10,M12,M16): ")
);strcase
);setq
(cond
((= a "M10") (prompt "\nYou have choosen M10"))
((= a "M12") (prompt "\nYou have choosen M12"))
((= a "M16") (prompt "\nYou have choosen M16"))
(T (prompt "\nUnknown Bolt Size"))
);cond
(princ)
);defun
(princ)
The (cond) function takes any number of lists as it's arguments.
Each argument must be a list containing a test followed by any number of
expressions to be evaluated. |