You are on page 1of 4

http://www.jeﬀerypsanders.com/autolispexp_...

Back to AutoLisp Advanced Tutorial HomePage Back to AutoLisp Tutorial HomePage Back to AutoLisp HomePage Back to JefferyPSanders.com

Conditional Statements:
if
Coming soon...

cond

if - This function evaluates an expression to decide which expressions to do
afterwards. Syntax : (if Syntax : (if thisIsTrue thisIsTrue thenDoThis) thenDoThis elseDoThis) (progn

A U T O L I S P A D V A N C E D
1 of 4

Syntax : (if thisIsTrue elseDoAllOfThis) ) Syntax : (if Syntax : (if thisIsTrue thisIsTrue

(progn thenDoAllOfThis)

(progn thenDoAllOfThis) elseDoThis) thenDoThis (progn elseDoAllOfThis) )

thisIsTrue - Any valid autolisp expression that evaluates to true or non nil. thenDoThis - Any valid autolisp expression. elseDoThis - Any valid autolisp expression. thenDoAllOfThis - Any valid autolisp expressions. elseDoAllOfThis - Any valid autolisp expressions. progn - Simply means there will be more than one statement here.

(if T (princ 3)) (if T (princ 3)(princ 4)) evaluates to True (if nil (princ 3)(princ 4)) evaluates to False T (if (= 3 3) (princ 3) (princ 4)) (if (= 3 4) (princ 3) (princ 4))

returns 3 returns 3 because T always returns 4 because nil always

(if (= nil T) (princ 3)(princ 4)) returns 4 because nil does not equal returns 3 returns 4

-------------------------------------------------------------------------------------------------(if (= 3 3) (progn (princ 3) returns 3

11/01/2011 10:13 PM

returns 5 T U T O R I A L A U T O L I S P not equal 4 ) ) --------------------------------------------------------------------------------------------------(if (= 3 3) (progn (princ 3) (princ 5) ) (progn (princ 8) here because 3 = 3 (princ 9) because 3 = 3 ) ) -------------------------------------------------------------------------------------------------(if (= 3 4) (progn (princ 3) (princ 5) not equal 4 ) (progn (princ 8) (princ 9) ) prints 8 prints 9 program never gets inside here because 3 does program never gets inside here because 3 does program never gets inside here program never gets inside returns 3 returns 5 A D V A N 2 of 4 ) cond .This function test conditions until one of them is true..jeﬀerypsanders.. At that point it exits the function.The AutoLisp Advanced Tutorial (princ 5) http://www. Syntax : (cond (ifThisIsTrue thenDoThis) thenDoThis) thenDoThis) (elseIfThisIsTrue (elseIfThisIsTrue 11/01/2011 10:13 PM .com/autolispexp_.

elseDoThis ..Any valid autolisp expression that evaluates to true or non nil.Any valid autolisp expressions. ) Syntax : (cond (ifthisIsTrue (progn thenDoAllOfThis) ) thenDoThis) (progn thenDoAllOfThis) ) thenDoThis)) (progn thenDoAllOfThis) ) thenDoThis)) C E D T U T O R I A L A U T O L I S P A D 3 of 4 point point (elseIfThisIsTrue (elseIfthisIsTrue (elseIfthisIsTrue (elseIfthisIsTrue (elseIfthisIsTrue ) thisIsTrue .. progn . (cond ( (= 1 1) (princ "True") ) ( (= 1 2) (princ "True") ) ) prints TRUE and exits doesn't make it to this returns True -------------------------------------------------------------------------------------------------(cond ( (= 1 0) (princ "True") ) equal 0 ( (= 1 1) (princ "True") ) ( (= 1 2) (princ "True") ) ) prints TRUE and exits doesn't make it to this returns True skips because 1 does not -------------------------------------------------------------------------------------------------(cond ( (= 4 3) (princ "True") ) equal 3 ( (= 4 2) (princ "True") ) equal 2 ( (= 4 1) (princ "True") ) equal 1 skips because 4 does not skips because 4 does not skips because 4 does not 11/01/2011 10:13 PM .Any valid autolisp expression.com/autolispexp_. elseDoAllOfThis .Simply means there will be more than one statement here.jeﬀerypsanders.Any valid autolisp expressions.Any valid autolisp expression. thenDoThis .The AutoLisp Advanced Tutorial http://www. thenDoAllOfThis .

All rights reserved.The AutoLisp Advanced Tutorial ) http://www.com..com Last Updated September 28th. prints "Nothing" returns "Nothing" sets variable [a] to skips because 4 does not skips because 4 does not skips because 4 does not set a variable 4 of 4 11/01/2011 10:13 PM .. returns nil -------------------------------------------------------------------------------------------------(cond ( (= 4 3) (princ "True") ) equal 3 skips because 4 does not skips because 4 does not skips because 4 does not prints "Nothing" because returns "Nothing" V A N C E D T U T O R I A L ( (= 4 2) (princ "True") ) equal 2 ( (= 4 1) (princ "True") ) equal 1 ( T (princ "Nothing") ) T = True ) -------------------------------------------------------------------------------------------------(setq a (cond ( (= 4 3) (princ "True") ) equal 3 ( (= 4 2) (princ "True") ) equal 2 ( (= 4 1) (princ "True") ) equal 1 ( T (princ "Nothing") ) because T = True ) ) "Nothing" End of Conditional Statements AutoLisp Advanced Tutorial AutoLisp Tutorial AutoLisp Home Home All questions/complaints/suggestions should be sent to JefferyPSanders. 2008 Copyright 2002-2008 JefferyPSanders.jeﬀerypsanders.com/autolispexp_.