The AutoLisp Advanced Tutorial


Conditional Statements:

if    cond   


Coming soon...

if - This function evaluates an expression to decide which expressions to do afterwards.

          Syntax : (if    thisIsTrue   thenDoThis)

          Syntax : (if    thisIsTrue   thenDoThis    elseDoThis)

          Syntax : (if    thisIsTrue   (progn thenDoAllOfThis)   (progn   elseDoAllOfThis) )

          Syntax : (if    thisIsTrue   (progn thenDoAllOfThis)  elseDoThis)

          Syntax : (if    thisIsTrue   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))                                       returns 3

                (if T (princ 3)(princ 4))                   returns 3 because T always evaluates to True

                (if nil (princ 3)(princ 4))               returns 4 because nil always evaluates to False

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

                (if (= 3 3) (princ 3) (princ 4))   returns 3

                (if (= 3 4) (princ 3) (princ 4))   returns 4

                 --------------------------------------------------------------------------------------------------

                (if (= 3 3)

          (progn

              (princ 3)                                    returns 3

              (princ 5)                                        returns 5

                    )

                 )

                  ---------------------------------------------------------------------------------------------------

                (if (= 3 3)       

                    (progn

                        (princ 3)                   returns 3

                        (princ 5)                                       returns 5

                    )

                         (progn

                                  (princ 8)                                         program never gets inside here because 3 = 3

                      (princ 9)                                      program never gets inside here because 3 = 3

                          )

                 )

                 --------------------------------------------------------------------------------------------------

                (if (= 3 4)       

                    (progn

                        (princ 3)       program never gets inside here because 3 does not equal 4

                        (princ 5)            program never gets inside here because 3 does not equal 4

                    )

                         (progn

                                  (princ 8)                                         prints 8

                      (princ 9)                                      prints 9

                          )

                 )

 

 

cond - This function test conditions until one of them is true.  At that point it exits the function.

          Syntax : (cond 

                            (ifThisIsTrue    thenDoThis)

                            (elseIfThisIsTrue    thenDoThis)

                            (elseIfThisIsTrue    thenDoThis)

                       )

          Syntax : (cond

                             (ifthisIsTrue    (progn thenDoAllOfThis) )

                            (elseIfThisIsTrue    thenDoThis)

                             (elseIfthisIsTrue    (progn thenDoAllOfThis) )

                             (elseIfthisIsTrue    thenDoThis))

                             (elseIfthisIsTrue    (progn thenDoAllOfThis) )

                             (elseIfthisIsTrue    thenDoThis))

                       )

               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.

 

                (cond

          ( (= 1 1) (princ "True") )                       prints TRUE and exits

                       ( (= 1 2) (princ "True") )                       doesn't make it to this point

                 )                                        returns True 

                 --------------------------------------------------------------------------------------------------

                (cond

          ( (= 1 0) (princ "True") )                       skips because 1 does not equal 0

                       ( (= 1 1) (princ "True") )                       prints TRUE and exits

                       ( (= 1 2) (princ "True") )                       doesn't make it to this point

                 )                                                                                      returns True

 

                 --------------------------------------------------------------------------------------------------

                (cond

          ( (= 4 3) (princ "True") )                       skips because 4 does not equal 3

                       ( (= 4 2) (princ "True") )                       skips because 4 does not equal 2

                       ( (= 4 1) (princ "True") )                       skips because 4 does not equal 1

                 )                                      returns nil

                 --------------------------------------------------------------------------------------------------

                (cond

          ( (= 4 3) (princ "True") )                       skips because 4 does not equal 3

                       ( (= 4 2) (princ "True") )                       skips because 4 does not equal 2

                       ( (= 4 1) (princ "True") )                       skips because 4 does not equal 1

                       ( T (princ "Nothing") )                              prints "Nothing" because T = True

                 )                                      returns "Nothing"

                 --------------------------------------------------------------------------------------------------

                (setq a                                 set a variable

                  (cond

           ( (= 4 3) (princ "True") )                       skips because 4 does not equal 3

                         ( (= 4 2) (princ "True") )                       skips because 4 does not equal 2

                         ( (= 4 1) (princ "True") )                       skips because 4 does not equal 1

                         ( T (princ "Nothing") )                              prints "Nothing" because T = True

                     )                                     returns "Nothing"

                 )                                         sets variable [a] to "Nothing"

End of Conditional Statements


AutoLisp Advanced Tutorial

AutoLisp Tutorial